7.Bean的自动装配
自动装配是spring满足bean依赖的一种方式!
spring会在上下文中自动寻找,并自动给bean进行装配!
在spring中有三种装配方式:
(1) 在xml文件中显示的配置
(2) 在Java中显示的配置
(3) 隐式的自动装配bean 【重要】
1.测试
一个人有猫和狗!!
2.byName
会在容器上下文中进行查找,和自己对象set方法后面的值对应的beanid!(不区分大小写!)
<?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="dog" class="com.zuo.dao.Dog"/>
<bean id="cat" class="com.zuo.dao.Cat"/>
<bean id="person" class="com.zuo.dao.Person" autowire="byName">
<property name="name" value="zuoshikun"/>
</bean>
</beans>
3.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.zuo.dao.Dog"/>
<bean id="cat" class="com.zuo.dao.Cat"/>
<bean id="person" class="com.zuo.dao.Person" autowire="byType">
<property name="name" value="zuoshikun"/>
</bean>
</beans>
小结:
在使用byName时,需要保证bean的id唯一,要注意bean要和自动注入的属性的set方法的值一致!
在使用byType时,需要保证bean的class(类型)唯一,要注意bean要和自动注入的属性的类型一致!
4.使用注解自动装配
jdk1.5支持注解,spring2.5支持注解
为什么使用注解:
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.
要使用注解的须知:
1.导入约束
2.配置注解支持
<?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
- 直接在属性上使用即可!也可以在set方式上使用!
- 可以省略不写set方法,前提是你这个自动装配的属性在IOC(Spring)容器
科普:
(1)//如果显示定义了Autowired的required属性为false,说明这个对象可以为ull,否则不允许为空
@Autowired(required = false)
(2)@Nullable 字段标记了这个注解,说明这个字段可以为nu11;
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解[@Autowired] 完成的时候、
我们可以使用@Qualifier(value="xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!
public class Person {
private String name;
@Autowired
@Qualifier(value = "dog222")
private Dog dog;
private Cat cat;
}
@Resource
public class Person {
private String name;
private Dog dog;
@Resource(name = "cat1")
private Cat cat;
}
小结:
1.@Autowired和@Resource的区别
都是用来自动装配的,都可以用在字段上
@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调优的?