在Spring中bean有三种装配机制,分别是:
Spring的自动装配需要从两个角度来实现,或者说是两个操作:
组件扫描和自动装配组合发挥巨大威力,使得显示的配置降低到最少。
推荐不使用自动装配xml配置,而使用注解。
新建一个项目
创建实体类:一个人有两个宠物
// 猫
public class Cat {
public void shout(){
System.out.println("喵喵喵");
}
}
// 狗
public class Dog {
public void shout(){
System.out.println("汪汪汪");
}
}
// 人
public class People {
private String name;
private Cat cat;
private Dog dog;
// 构造
// toString
// get、set
}
编写Spring配置文件
<?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.jh.domain.Cat"/>
<bean id="dog" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People">
<property name="name" value="姓名"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
测试环境是否正常
@Test
public void test() throws Exception{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
autowire byName (按名称自动装配)
由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。
采用自动装配将避免这些错误,并且使配置简单化。具体方法如下:
修改bean配置,增加一个属性 autowire="byName"
<bean id="cat" class="com.jh.domain.Cat"/>
<bean id="dog" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People" autowire="byName">
<property name="name" value="姓名"/>
</bean>
执行测试,发现依然执行了方法!
将 cat 的bean id修改为 catXXX,执行测试
执行时报空指针java.lang.NullPointerException。
因为按byName规则找不对应的set方法,真正的setCat就没执行,对象就没有初始化,所以调用时就会报空指针错误。
由此我们可以看出,byName的执行过程:
autowire byType (按类型自动装配)
使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。
NoUniqueBeanDefinitionException
具体使用方式如下:
改bean配置,增加一个属性 autowire="byType"
<bean id="cat" class="com.jh.domain.Cat"/>
<bean id="dog" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People" autowire="byType">
<property name="name" value="姓名"/>
</bean>
执行测试,正常输出
再添加一个dog的bean,执行测试
<bean id="cat" class="com.jh.domain.Cat"/>
<bean id="dog" class="com.jh.domain.Dog"/>
<bean id="dog2" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People" autowire="byType">
<property name="name" value="姓名"/>
</bean>
发现报错NoUniqueBeanDefinitionException
删除新增的dog bean ,将其他的bean id都删除
<bean class="com.jh.domain.Cat"/>
<bean class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People" autowire="byType">
<property name="name" value="姓名"/>
</bean>
执行,正常输出!
通过上面的例子,我们可以发现:
小结:
jdk1.5开始支持注解,spring2.5开始全面支持注解。
?基于注释的配置的引入提出了一个问题,即这种方法是否比XML"更好"。简短的回答是"视情况而定"。长的回答是,每种方法都有其优缺点,通常由开发人员决定哪种策略更适合他们。
要使用注解,需要有如下的准备工作:
导入约束:context约束
配置注解的支持:【很重要】
<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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
具体使用:
在实体类的属性上添加@Autowired注解
public class People {
private String name;
@Autowired
private Cat cat;
@Autowired
private Dog dog;
public People() {
}
public People(String name, Cat cat, Dog dog) {
this.name = name;
this.cat = cat;
this.dog = dog;
}
@Override
public String toString() {
return "People{" +
"name=‘" + name + ‘\‘‘ +
", cat=" + cat +
", dog=" + dog +
‘}‘;
}
public String getName() {
return name;
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
}
修改spring配置文件
<?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:comtext="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 https://www.springframework.org/schema/context/spring-context.xsd">
<comtext:annotation-config/>
<bean id="cat1" class="com.jh.domain.Cat"/>
<bean id="dog1" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People"/>
</beans>
测试,注入成功!
【拓展】
@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。
// 如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat;
@Nullable,可以用于方法,属性,表示可以为null
// @Nullable,表示当前属性可以为null
public People(@Nullable String name) {
this.name = name;
}
@Qualifier注解:它无法单独使用,可以配合其他注解
@Autowired是根据类型自动装配的,如果有多个相同的对象,将无法使用。
但是,加上@Qualifier则可以根据byName的方式自动装配。
具体步骤:
修改Bean,一个类有多个对象,且id不为类的默认值!
<bean id="cat1" class="com.jh.domain.Cat"/>
<bean id="cat2" class="com.jh.domain.Cat"/>
<bean id="dog1" class="com.jh.domain.Dog"/>
<bean id="dog2" class="com.jh.domain.Dog"/>
如果没有@Qualifier,编译器报错。
在属性上添加Qualifier注解,如下
public class People {
private String name;
@Autowired
@Qualifier("cat1")
private Cat cat;
@Autowired
@Qualifier("dog1")
private Dog dog;
}
加入了@Qualifier注解之后,不再报错
测试结果,成功!
【小结】如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value="xxx")去配合@Autowired的使用,指定一个唯一的bean对象注入!
@Resource不是spring的注解,属于 javax.annotation.Resource ;它也可以实现自动装配
具体实现:
编写实体类
public class People {
private String name;
@Resource(name = "cat1")
private Cat cat;
@Resource
private Dog dog;
}
applicationContext.xml
<bean id="cat1" class="com.jh.domain.Cat"/>
<bean id="cat2" class="com.jh.domain.Cat"/>
<bean id="dog1" class="com.jh.domain.Dog"/>
<bean id="dog" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People"/>
测试,结果符合预期!
修改配置文件:
<bean id="cat1" class="com.jh.domain.Cat"/>
<bean id="cat2" class="com.jh.domain.Cat"/>
<bean id="dog1" class="com.jh.domain.Dog"/>
<bean id="dog2" class="com.jh.domain.Dog"/>
<bean id="people" class="com.jh.domain.People"/>
测试后报错:关于dog的对象有2个,id都不是默认,无法装配dog
原文:https://www.cnblogs.com/paidaxing0623/p/14312223.html