自动装配AutoWire
- 自动装配是使用spring满足bean依赖的一种方法
- spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:
- 在xml中显式配置;
- 在java中显式配置;
- 隐式的bean发现机制和自动装配。
这里我们主要讲第三种:自动化的装配bean。
Spring的自动装配需要从两个角度来实现,或者说是两个操作:
- 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
- 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;
组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。
推荐不使用自动装配xml配置 , 而使用注解 .
<!--
在spring中,可以使用自动装配功能,spring会把某些bean注入到另外bean中
可以使用autowire属性实现自动装配,有以下几种情况
default/no:不装配
byName:按照id来进行装配,根据set方法后面的名称的首字母小写决定的,不是参数列表的名称
byType:按照bean的类型来进行装配,但是如果有多个类型,就会报错,不知道选择哪一个具体的类型
constructor:按照构造器进行装配,首先按照类型进行判断,如果有多个类型相同的bean,再按照id去进行判断
-->
7.1、测试
环境搭建:一个人有两个宠物
1、新建猫、狗、人类
public class Cat {
public void shout(){
System.out.println("苗苗");
}
}
public class Dog {
public void shout(){
System.out.println("旺旺");
}
}
public class People {
private Cat cat;
private Dog dog;
private String name;
// 默认无参构造,set、get、toString()
2、配置Beans.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="cat" class="com.lian.pojo.Cat"/>
<bean id="dog" class="com.lian.pojo.Dog"/>
<bean id="people" class="com.lian.pojo.People">
<property name="name" value="pidan"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
3、测试
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
7.2、自动装配 byName
<?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="cat" class="com.lian.pojo.Cat"/>
<bean id="dog" class="com.lian.pojo.Dog"/>
<!--
测试自动装配 byName
需要保证所有bean的id唯一
将查找其类中所有set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat
去spring容器中寻找是否有此字符串名称id的对象
会自动在容器上下文中查找,和自己对象的set方法后面的值的对应的beanid 例如:Setcat和cat
-->
<bean id="people" class="com.lian.pojo.People" autowire="byName">
<property name="name" value="pidan"/>
</bean>
</bean>
7.3、自动装配 byType
<?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="cat" class="com.lian.pojo.Cat"/>
<bean id="dog" class="com.lian.pojo.Dog"/>
<!--
测试自动装配 byType
需要保证所有bean的class唯一,同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常
会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.lian.pojo.People" autowire="byType">
<property name="name" value="pidan"/>
</bean>
<!--注意:byType情况下,id可以省略不写,因为只是根据类型查找-->
<bean class="com.lian.pojo.People" autowire="byType">
<property name="name" value="pidan"/>
</bean>
</bean>
小结:
byname:需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
byType:需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
7.4、自动装配 使用注解实现
准备工作:利用注解的方式注入属性。
1、在spring配置文件中引入context命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
2、开启属性注解支持!
<context:annotation-config/>
演示操作:
1、People类
public class People {
/**
* @Autowired(required = false)
* boolean required() default true;
* 如果显示定义了 Autowired 的required属性为false,说明这个对象可以为null,否则不允许为空
*
* Autowired:从 beans.xml 中查找,先查找和属性名相同的bean的 id 名,再查找和属性名相同的 class全类名类型
* 1、根据名称查找(根据属性的名称是cat,到beans.xml中查找符合属性cat的 bean的id名称,如果找不到则在根据类型查找)
* 2、根据类型查找(根据属性的类型是Cat,到beans.xml中查找符合属性cat的 全类名class类型,且bean里面的 * class全类名类型必须唯一,autowired才能找到。
* 3、如果,beans.xml中有2个及以上相同的bean(bean的id名和全类名类型均相同),autowired就找不到了
*/
@Autowired
private Cat cat;
/**
* Qualifier 不能单独使用,必须和autowired搭配使用
* @Qualifier(value = "") 默认为空
* Qualifier 根据名称查找(根据setCat方法,去除set,cat小写,去bean里找id名称为cat)
* 如果多个bean的类型相同,但bean的id名称不同,则可以用qualifier查找指定id名称
*/
@Autowired
@Qualifier(value = "dog_taidi")
private Dog dog;
private String name;
2、beans.xml配置
需要导入 context 和 aop 约束
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
<bean id="cat" class="com.lian.pojo.Cat"/>
<bean id="dog" class="com.lian.pojo.Dog"/>
<bean id="people2" class="com.lian.pojo.People"/>
</beans>
3、测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
People people = context.getBean("people2", People.class);
people.getCat().shout();
people.getDog().shout();
}
4、测试reource
/**
* @Resource 集齐了autowired和qualifier的功能,既可根据类型查找,也可根据名称查找
*/
@Resource(name = "cat111")
private Cat cat;
小结
@Autowired与@Resource异同:
1、@Autowired与@Resource都可以用来装配bean。都可以写在字段上,或写在setter方法上。
2、@Autowired默认按类型装配(属于spring规范),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用
3、@Resource(属于J2EE复返),默认按照名称进行装配,名称可以通过name属性进行指定。如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
它们的作用相同都是用注解方式注入对象,但执行顺序不同。@Autowired先byType,@Resource先byName。
扩展
SpEL的使用
SpEL:Spring Expression Language,spring的表达式语言,支持运行时查询操作对象
使用#{…}作为语法规则,所有的大括号中的字符都认为是SpEL.
ioc.xml
<bean id="person4" class="com.mashibing.bean.Person">
<!--支持任何运算符-->
<property name="age" value="#{12*2}"></property>
<!--可以引用其他bean的某个属性值-->
<property name="name" value="#{address.province}"></property>
<!--引用其他bean-->
<property name="address" value="#{address}"></property>
<!--调用静态方法-->
<property name="hobbies" value="#{T(java.util.UUID).randomUUID().toString().substring(0,4)}"></property>
<!--调用非静态方法-->
<property name="gender" value="#{address.getCity()}"></property>
</bean>