Spring 实例化 bean 的形式
- Set 注入
- 结构器注入
- 动态工厂注入
- 实例化工厂注入
案例实操
Set 注入
xml 配置(同时 spring 也提供了对于根本数据类型的 set 注入形式)
<?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="userDao" class="com.xxx.demo.UserDao"></bean>
<!-- setter 注入 -->
<bean id="userService" class="com.xxx.demo.UserService">
<!--ref 是对于内部 bean 对象援用,与被援用的 bean 对象的 id 保持一致 -->
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
UserDao.java
public class UserDao {public String userLogin() {return "我是 UserDao 中的 userLogin() 的办法";
}
}
UserService.java
public class UserService {
// 肯定要提供属性的 setter 办法
private UserDao userDao;
public void userlogin() {String res=userDao.userLogin();
System.out.println(res);
}
public void setUserDao(UserDao userDao) {this.userDao = userDao;}
}
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {public static void main(String[] args) {ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService=applicationContext.getBean("userService", UserService.class);
userService.userlogin();}
}
结构器注入
xml 配置(也提供对于根本数据类型、字符串等值的注入)
<?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="userDao" class="com.xxx.demo.UserDao"></bean>
<!-- 结构器注入 -->
<bean id="userServiceV2" class="com.xxx.demo.UserServiceV2">
<constructor-arg index="0" ref="userDao"></constructor-arg>
<constructor-arg index="1" value="印度三哥"></constructor-arg>
</bean>
</beans>
结构器注入有三种模式:
index 属性为参数程序,如果只有一个参数 index 能够不设置。
name 属性依据结构器中属性的名字。
type 属性,是依据结构器中属性的类型来匹配的。如果雷同类型属性不惟一,注入的属性依照程序注入进来。
UserServiceV2.java 类提供构造函数
/**
* 实现结构器注入
* @author Best Liu
*
*/
public class UserServiceV2 {
private UserDao userDao;
private String name;
public void userlogin() {String res=userDao.userLogin();
System.out.println(res);
System.out.println(name);
}
public UserServiceV2(UserDao userDao,String name) {super();
this.userDao = userDao;
this.name = name;
}
}
动态工厂注入
xml 配置
<?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="userDao01" class="com.xxx.demo.StaticFactory" factory-method="createuserDao"></bean>
<bean id="userService01" class="com.xxx.demo.UserService">
<property name="userDao" ref="userDao01"></property>
</bean>
</beans>
StaticFactory.java
public class StaticFactory {public static UserDao createuserDao(){return new UserDao();
}
}
UserService.java
public class UserService {
private UserDao userDao;
public void userlogin() {String res=userDao.userLogin();
System.out.println(res);
}
public void setUserDao(UserDao userDao) {this.userDao = userDao;}
}
tips: 动态工厂注入就是 IoC 动态工厂和 DI 的 setter 注入,将须要注入的属性对象利用动态工厂创立进去.
2.4 实例化工厂
xml 配置
<?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="instanceFactory" class="com.xxx.demo.InstanceFactory"></bean>
<bean id="userDao3" factory-bean="instanceFactory" factory-method="createUserDao"></bean>
<bean id="userService02" class="com.xxx.demo.UserService">
<property name="userDao" ref="userDao3"></property>
</bean>
</beans>
InstanceFactory.java
public class InstanceFactory {public UserDao createUserDao(){return new UserDao();
}
}
tips: 重点把握 set,结构器注入,工厂形式理解即可,理论开发中根本应用 set 形式注入 bean。
扩大
循环依赖的问题的产生
Bean 通过结构器注入,之间彼此相互依赖对方导致 bean 无奈实例化。
注入的抉择:开发我的项目中 set 形式注入首选
应用结构注入能够在构建对象的同时一并实现依赖关系的建设,对象一建设则所有的所有也就筹备好了,但如果要建设的对象关系很多,应用结构注入会在构建函数上留下一长串的参数,且不易记忆,这时应用 Set 注入会是个不错的抉择。
应用 Set 注入能够有明确的名称,能够理解注入的对象会是什么,像 setxxx() 这样的名称比记忆 Constructor 上某个参数的地位代表某个对象更好。
xml 配置
<?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="goodsService" class="com.xxx.demo.GoodsService">
<!-- <constructor-arg index="0" ref="userService"></constructor-arg> -->
<property name="userService" ref="userService"></property>
</bean>
<bean id="userService" class="com.xxx.demo.UserService">
<!-- <constructor-arg index="0" ref="goodsService"></constructor-arg> -->
<property name="goodsService" ref="goodsService"></property>
</bean>
</beans>
GoodsService.java
public class GoodsService {
private UserService userService;
/*public GoodsService(UserService userService) {super();
this.userService = userService;
}*/
public void setUserService(UserService userService) {this.userService = userService;}
}
UserService.java
public class UserService {
private GoodsService goodsService;
/* public UserService(GoodsService goodsService) {super();
this.goodsService = goodsService;
}
*/
public void setGoodsService(GoodsService goodsService) {this.goodsService = goodsService;}
}