spring的依赖注入的三种方式
- 使用构造函数提供
- 使用set方法提供
- 使用注解提供
使用构造函数提供
使用标签:constructor-arg
使用位置:<bean>
标签内部
使用例子:
<!-- constructor-arg标签的部分属性
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始
name:用于指定给构造函数中指定名称的参数赋值 这个比较常用常用的
优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供
-->
<bean id="addressService" class="service.impl.AddressService">
<constructor-arg name="aa" value="12"/>
<constructor-arg name="bb" value="bb"/>
<constructor-arg name="date" ref="new"/>
</bean>
<!-- 配置一个日期对象
读取这个全限定类型,反射创建一个对象,并且存入spring的核心容器中,我们可以用bean的id来把这个对象取出来
-->
<bean id="new" class="java.util.Date"/>
AddressService.java文件
public class AddressService implements IAddressService {
private Integer aa;
private String bb;
private Date date;
public AddressService(Integer aa, String bb, Date date) {
this.aa = aa;
this.bb = bb;
this.date = date;
}
public void saveAddress() {
System.out.println("AddressService{" +
"aa=" + aa +
", bb='" + bb + '\'' +
", date=" + date +
'}');
}
}
使用set方法进行依赖注入
使用标签:property
使用位置:<bean>
标签内部
使用例子:
<!--
property属性的name标签是指set方法后面的东西,大小写限制
-->
<bean id="new" class="java.util.Date" />
<bean id="addressService" class="service.impl.AddressService">
<property name="aa" value="12"/>
<property name="bb" value="bb"/>
<property name="date" ref="new"/>
<!-- 如果是map集合的话可以这样子写-->
<property name="map">
<map>
<entry key="1" value="1111"/>
<entry key="2" value="2222"/>
<entry key="3" value="3333"/>
</map>
</property>
<!-- 不是map集合这样子写-->
<property name="set">
<set>
<value>1111</value>
<value>1111</value>
<value>1111</value>
</set>
</property>
</bean>
AddressService.java文件
public class AddressService implements IAddressService {
private Integer aa;
private String bb;
private Date date;
private Map<String,Integer> map;
public void setAa(Integer aa) { this.aa = aa;}
public void setBb(String bb) {this.bb = bb;}
public void setDate(Date date) {this.date = date;}
public void setMap(Map<String, Integer> map) { this.map = map; }
public void saveAddress() {
System.out.println("AddressService{" +
"aa=" + aa +
", bb='" + bb + '\'' +
", date=" + date +
"map=" + map +
'}');
}
}
使用注解方式注入依赖
- 使用注解:
@Component: 把普通的pojo实例化到spring容器中,相当于<bean id="addressService" class="service.impl.AddressService" />,当组件不容易分类是使用这个
@Controller: 一般用于表现层
@Service: 一般用于业务层
@Repository: 一般用于持久层
@Component @Controller @Service @Repository这三个注解都有一个value属性,value值是指注入依赖的id名称,value属性可以不写,默认使用依赖名,依赖名首字母小写
这三个使三层对象更加清晰
@Autowired: 按照类型注入,只要容器中有唯一一个bean对象和要注入的变量类型匹配,就可以注入成功,
如果一个也没有则报错,
如果有多个匹配,则使用@Autowired+@Qualifier(value = "容器中bean对象的id")结合的方式进行处理,
或者使用@Resource(name = "容器中bean对象的id")的方式进行处理
@Autowired @Qualifier @Resource ---->这三个注解都只能注入bean类型的数据,基本类型和string类型无法使用上述注解
@Value 注解可以对基本类型和string类型实现注入
在使用注解时,要告知spring创建容器时要使用的包,配置一个bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 告知spring在创建容器时要扫描的包-->
<context:component-scan base-package="service.impl"/>
<context:component-scan base-package="dao.impl"/>
<context:component-scan base-package="controller"/>
</beans>
使用例子:
@Repository(value = "addressDao")
public class AddressDaoImpl implements IAddressDao {
public void saveAddress(){
System.out.println("模拟打印1111111");
}
}
@Service(value = "addressService")
public class AddressService implements IAddressService {
// @Autowired
// @Qualifier(value = "addressDao")
@Resource(name = "addressDao2")
private IAddressDao iAddressDao;
public void saveAddress() {
iAddressDao.saveAddress();
}
}
@Controller
public class Cline {
/**
* 获取spring的ioc核心容器,并根据id获取对象
* ApplicationContext的三个常用实现类
* ClassPathXmlApplicationContext:加载类路径下的配置文件,要求配置文件必须在类路径下,这种方式相对于第二个更常用。
* FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件
* AnnotationConfigApplicationContext:用于读取注解创建容器
* @param args
*/
public static void main(String[] args) {
// 获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
// 根据id获取bean对象
IAddressService addressService = (IAddressService)ac.getBean("addressService");
addressService.saveAddress();
}
}