Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配的方式
- 在xml中显式的配置
- 在java中显式配置
- 隐式的自动装配bean【重要】
搭建环境:一个人有两个宠物
people
package com.wx.pojo;
public class People {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
cat
package com.wx.pojo;
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
dog
package com.wx.pojo;
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
ByName自动装配
我们在bean标签中添加 autowire="byName"
属性,然后就可以通过自己对象set后面的值对象我们bean的id自动装配
<?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.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People" autowire="byName">
<property name="name" value="wx"/>
</bean>
</beans>
测试
import com.wx.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout();
people.getCat().shout();
}
}
ByType自动装配
我们在bean标签中添加 autowire="byType"
属性,会自动在容器上下文查找,和自己对象属性类型相同的bean
<?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.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People" autowire="byType">
<property name="name" value="wx"/>
</bean>
</beans>
测试
import com.wx.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getDog().shout();
people.getCat().shout();
}
}
小结
- 使用ByName的时候,需要保证所有bean的id唯一,并且bean需要和自动配置的属性的set方法的值一致
- 使用ByType的时候,需要保证bean的class唯一,并且这个bean需要和自动注入的属性类型一致
实现注解实现自动装配
jdk1.5支持的注解,Spring2.5支持注解
要使用注解需知:
- 导入约束
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- 配置注解的支持
<context:annotation-config/>
<?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="cat" class="com.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People"/>
</beans>
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
@Autowired(required=false) 说明: false,对象可以为null;true,对象必须存对象,不能为null。
package com.wx.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
Qualifier
- @Autowired是根据ByType自动装配的,加上@Qualifier则可以根据ByName的方式自动装配
- @Qualifier不能单独使用。
测试
添加几个id不一样类一样的对象
<bean id="cat" class="com.wx.pojo.Cat"/>
<bean id="cat1" class="com.wx.pojo.Cat"/>
<bean id="dog" class="com.wx.pojo.Dog"/>
<bean id="dog1" class="com.wx.pojo.Dog"/>
<bean id="people" class="com.wx.pojo.People"/>
没有加入Qualifier注解运行直接报错
加入Qualifier注解
public class People {
@Autowired
@Qualifier(value = "cat")
private Cat cat;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
测试
Resource
这个注解在不是spring的,是jdk的,在高版本的jdk已经取消了
- @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
- 其次再进行默认的byName方式进行装配;
- 如果以上都不成功,则按byType的方式自动装配。
- 都不成功,则报异常。
public class People {
@Resource(name = "cat")
private Cat cat;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
private String name;
小结:
@Resource与 @Autowired区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过ByType的方式实现
- @Resource如有指定的name属性,先按该属性进行byName方式查找装配,其次再进行默认的byName方式进行装配 如果以上都不成功,则按byType的方式自动装配。