版权声明:有一种生活不去经历不知其中艰辛,有一种艰辛不去体会,不会知道其中快乐,有一种快乐,没有拥有不知其中纯粹 https://blog.csdn.net/wwwzydcom/article/details/82858598
大型的项目更多的是面向接口编程,依赖没有实现,比如支付的实现,多个支付方式定义了不同接口,依赖的不是具体的实现,而是依赖接口,如果通过
ew实例化,比较固定,代码维护难度大,改成外部传入,传入的时候动态改变实现
面向接口编程:通过模块的划分,出现大量接口。设计接口,其他模块通过实现接口,实现模块之间的相互调用
IOC的注入方式:
1.set注入:
name属性名 ref:引用的某个bean的id标识
<!-- set注入-->
<bean id="userService" class="com.sxt.service.UserService">
<property name="userDao" ref="userDao"></property>
<property name="flag" value="false"></property>
<property name="host" value="110110110"></property>
<property name="port" value="8888"></property>
</bean>
<bean id="userDao" class="com.sxt.dao.UserDao"></bean>
//set注入
@Test
public void test04(){
UserService userService = null;
BeanFactory factory = new ClassPathXmlApplicationContext("spring04.xml");
userService = (UserService) factory.getBean("userService");
userService.test();
}
package com.sxt.service;
import com.sxt.dao.UserDao;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public UserService() {
}
private String host;
private Integer port;
private Boolean flag;
public void setHost(String host) {
this.host = host;
}
public void setPort(Integer port) {
this.port = port;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
public UserService(String host) {
this.host = host;
}
public void test(){
userDao.test();
System.out.println("UserService.test..."+ host+"---"+"port"+flag);
}
}
package com.sxt.dao;
public class UserDao {
public void test(){
System.out.println("UserDao.test...");
}
}
第二种:构造器注入的方式
package com.sxt.service;
import com.sxt.dao.AccountDao;
import com.sxt.dao.UserDao;
public class UserService02 {
private UserDao userDao;
private AccountDao accountDao;
private String host;
private Integer port;
public UserService02(UserDao userDao, AccountDao accountDao, String host, Integer port) {
this.userDao = userDao;
this.accountDao = accountDao;
this.host = host;
this.port = port;
}
public void test(){
userDao.test();
accountDao.test();
System.out.println("UserService.test..."+host+"--"+port);
}
}
创建对象时的直接根据name值
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- set注入-->
<bean id="userService02" class="com.sxt.service.UserService02">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
<constructor-arg name="host" value="15252"></constructor-arg>
<constructor-arg name="port" value="8888"></constructor-arg>
</bean>
<bean id="userDao" class="com.sxt.dao.UserDao"></bean>
<bean id="accountDao" class="com.sxt.dao.AccountDao"></bean>
</beans>
创建对象时的索引
<bean id="userService02" class="com.sxt.service.UserService02">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
<constructor-arg name="host" value="15252"></constructor-arg>
<constructor-arg name="port" value="8888"></constructor-arg>
</bean>
<bean id="userDao" class="com.sxt.dao.UserDao"></bean>
<bean id="accountDao" class="com.sxt.dao.AccountDao"></bean>
//构造器注入
@Test
public void test05(){
UserService02 userService = null;
BeanFactory factory = new ClassPathXmlApplicationContext("spring05.xml");
userService = (UserService02) factory.getBean("userService02");
userService.test();
}
注意:通过构造器会出现循环引用
@Test
public void test07(){
UserService03 userService = null;
BeanFactory factory = new ClassPathXmlApplicationContext("spring07.xml");
userService = (UserService03) factory.getBean("userService03");
System.out.println(userService);
}
<bean id="userService03" class="com.sxt.service.UserService03">
<constructor-arg name="accountService" ref="accountService"></constructor-arg>
</bean>
<bean id="accountService" class="com.sxt.service.AccountService">
<constructor-arg name="userService03" ref="userService03"></constructor-arg>
</bean>
package com.sxt.service;
public class AccountService {
private UserService03 userService03;
public void setUserService03(UserService03 userService03) {
this.userService03 = userService03;
}
}
package com.sxt.service;
import com.sxt.dao.AccountDao;
import com.sxt.dao.UserDao;
public class UserService03 {
private AccountService accountService;
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
}
而set注入的方式,先初始化再注入,构造器注入,在配置流程上,bean中constructor-arg
可能出现循环引用的过程,互相等待
静态工厂注入
<!-- 静态工厂注入-->
<bean id="userService04" class="com.sxt.service.UserService04">
<property name="userDao" ref="userDao"></property>
</bean>
package com.sxt.service;
import com.sxt.dao.UserDao;
public class UserService04 {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void test(){
userDao.test();
System.out.println("userService04.test...");
}
}
@Test
//静态工厂的实现
public void test08(){
UserService04 userService = null;
BeanFactory factory = new ClassPathXmlApplicationContext("spring08.xml");
userService = (UserService04) factory.getBean("userService04");
userService.test();
}
实例化工厂的注入
<!--
实例化工厂注入 了解
-->
<bean id="userService05" class="com.sxt.service.UserService05">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="instanceFactory" class="com.factory.InstanceFactory"></bean>
<bean id="accountDao" factory-bean="instanceFactory" factory-method="getAccountDao"></bean>
package com.sxt.service;
import com.sxt.dao.AccountDao;
public class UserService05 {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void test(){
accountDao.test();
System.out.println("userService05.test...");
}
}
//实例化工厂的实现
@Test
public void test09(){
UserService05 userService = null;
BeanFactory factory = new ClassPathXmlApplicationContext("spring09.xml");
userService = (UserService05) factory.getBean("userService05");
userService.test();
}
对于集合List的注入
package com.sxt.service;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class UserService06 {
private List<String> hosts;
private Map <String,String> map;
private Properties prop; //Properties 支持key-value就像jdk
public void setHosts(List<String> hosts) {
this.hosts = hosts;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProp(Properties prop) {
this.prop = prop;
}
//循环打印prop!注意记!
public void printProp(){
for (Map.Entry<Object,Object> entry:prop.entrySet()) {
System.out.println(entry.getKey()+"--"+entry.getValue());
}
}
//循环打印map!注意记!
public void printMap(){
for (Map.Entry<String,String > entry : map.entrySet()){
System.out.println(entry.getKey()+"---"+entry.getValue());
}
}
//循环打印list
public void printList(){
for (String str:hosts){
System.out.println(str);
}
}
public void test(){
System.out.println("UserService05.test...");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService06" class="com.sxt.service.UserService06">
<property name="hosts">
<list>
<value>127.152.12</value>
<value>127.152.13</value>
<value>127.152.14</value>
</list>
</property>
<property name="map">
<map>
<entry>
<key>
<value>a</value>
</key>
<value>10</value>
</entry>
<entry>
<key><value>b</value></key>
<value>20</value>
</entry>
<entry>
<key><value>c</value></key>
<value>30</value>
</entry>
</map>
</property>
<property name="prop">
<props>
<prop key="1">hello1</prop>
<prop key="2">hello2</prop>
<prop key="3">hello3</prop>
</props>
</property>
</bean>
</beans>
@Test
public void test10(){
UserService06 userService06 = null;
BeanFactory factory = new ClassPathXmlApplicationContext("spring10.xml");
userService06 = (UserService06) factory.getBean("userService06");
userService06.printList();
System.out.println("--------------------------");
userService06.printMap();
System.out.println("--------------------------");
userService06.printProp();
}
最简单的方式:基于注解,实现赋值的操作
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--启用注解注入配置-->
<context:annotation-config />
<bean id="userService07" class="com.sxt.service.UserService07"></bean>
<bean id="userDao" class="com.sxt.dao.UserDao"></bean>
<bean id="ad" class="com.sxt.dao.AccountDao"></bean>
</beans>
package com.sxt.service;
import com.sxt.dao.AccountDao;
import com.sxt.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.Resource;
/**
* @Resource 注入(装配)bean时:
* 默认采用属性名称注入。不存在则按照class类型注入,如果指定了resource中的name属性值,必须按照name的值注入
* 声明级别:通常在字段上或者set上
* @Autowired 默认采用class类型注入
*/
public class UserService07 {
private UserDao userDao;
@Autowired
@Qualifier("ad")
private AccountDao ad;
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void test(){
userDao.test();
ad.test();
System.out.println("userService07.test...");
}
}
//使用注解自定装配
@Test
public void test11(){
BeanFactory factory=new ClassPathXmlApplicationContext("spring10.xml");
UserService07 userService07= (UserService07) factory.getBean("userService07");
userService07.test();
}