Bean的自动装配
7、Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文自动寻找,并自动给bean装配属性
在Spring中有三种装配的方式
- 在xml中显示配置
- 在java中显示配置
- 隐式的自动装配bean 【重要】
byType自动装配:byType会自动查找,和自己对象set方法参数的类型相同的bean
要保证所有的class唯一(类为全局唯一)
byName自动装配:byName会自动查找,和自己对象set对应的值对应的id
要保证所有id唯一,并且和set注入的值一致
<!-- 找不到id和多个相同class:找不到 id=cat的bean,且有两个Cat类-->
<bean id="cat1" class="com.xy.pojo.Cat"/>
<bean id="cat2" class="com.xy.pojo.Cat"/>
7.1 测试:自动装配
1.pojo的Cat类
public class Cat {
public void shut(){
System.out.println("miao");
}
}
2.pojo的Dog类
public class Dog {
public void shut(){
System.out.println("wow");
}
}
3.pojo的People类
package 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 + '\'' +
'}';
}
}
4.xml配置 -> 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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.xy.pojo.Cat"/>
<bean id="dog" class="com.xy.pojo.Dog"/>
<!--byType会在容器自动查找,和自己对象属性相同的bean
例如,private Dog dog; 那么就会查找pojo的Dog类,再进行自动装配
-->
<bean id="people" class="com.xy.pojo.People" autowire="byType">
<property name="name" value="cbh"></property>
</bean>
</beans>
xml配置 -> byName 自动装配
<bean id="cat" class="com.xy.pojo.Cat"/>
<bean id="dog" class="com.xy.pojo.Dog"/>
<!--byname会在容器自动查找,和自己对象set方法的set后面的值对应的id
例如:setDog(),取set后面的字符作为id,则要id = dog 才可以进行自动装配
-->
<bean id="people" class="com.xy.pojo.People" autowire="byName">
<property name="name" value="cbh"></property>
</bean>
byName只能取到小写,大写取不到
例如 : setDog(),取set后面的字符作为id,则要id = dog 才可以进行自动装配
7.2 使用注解实现自动装配
jdk1.5支持的注解,spring2.5支持的注解
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.(翻译:基于注释的配置的引入提出了一个问题,即这种方法是否比XML“更好”)
要在配置中导入
-
context约束
xmlns:context="http://www.springframework.org/schema/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>
7.2.1、@Autowired
默认是byType方式,如果匹配不上,就会byName
在属性上使用,也可以在set上使用
我们可以不用编写set方法了,前提是自动装配的属性在Spring容器里,且要符合ByName 自动装配
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
@Nullable 字段标记了这个注解,说明该字段可以为空
public name(@Nullable String name){ }
//源码
public @interface Autowired {
boolean required() default true;
}
如果定义了Autowire的require属性为false,说明这个对象可以为null,否则不允许为空(false表示找不到装配,不抛出异常)
7.2.2、@Qualifier
@Autowired不能唯一装配时,需要@Autowired+@Qualifier
如果@Autowired自动装配环境比较复杂。自动装配无法通过一个注解完成的时候,可以使用@Qualifier(value = “dog”)去配合使用,指定一个唯一的id对象
public class People {
@Autowired
private Cat cat;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
private String name;
}
如果xml文件中同一个对象被多个bean使用,Autowired无法按类型找到,可以用@Qualifier指定id查找
7.2.3、@Resource
默认是byName方式,如果匹配不上,就会byType
public class People {
Resource(name="cat")
private Cat cat;
Resource(name="dog")
private Dog dog;
private String name;
}
Autowired是byType,@Autowired+@Qualifier = byType || byName
Autowired是先byteType,如果唯一則注入,否则byName查找。resource是先byname,不符合再继续byType
区别:
@Resource和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byType的方式实现,而且必须要求这个对象存在!【常用】
- @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!【常用】
- 执行顺序不同:@Autowired通过byType的方式实现。@Resource默认通过byname的方式实现
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?