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.or...d">
<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.or...d">
<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.or...d">
<!-- 静态工厂注入 -->
<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.or...d">
<!-- 实例化工厂 -->
<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.or...d">
<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;
}
}