【Spring Framework】7、自动装配
1、自动装配
- 自动装配是Spring满足bean以来的一种方式!
- Spring会在上下文中自动寻找,并自动给bean 配置属性!
在Spring中有三种装配的方式:
- 在xml中显示的配置
- 在Java中显示配置
- 隐式的自动装配bean【重要】
1.1、测试环境搭建
环境搭建 : 一个人有两个宠物
People
package com.xg.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.xg.pojo;
public class Cat {
public void shout(){
System.out.println("喵喵喵!");
}
}
Dog
package com.xg.pojo;
public class Dog {
public void shout(){
System.out.println("汪汪汪!");
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.xg.pojo.Cat"/>
<bean id="dog" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People">
<property name="name" value="遇见星光"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
测试类
import com.xg.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
1.2、ByName自动装配
byName : 会自动在容器上下文中查找,和自己对象 set方法 后面的值对应的 bean id !
如果没有找到将会报错!
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.xg.pojo.Cat"/>
<bean id="dog" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People" autowire="byName">
<property name="name" value="遇见星光"/>
</bean>
</beans>
1.3、ByType自动装配
byName : 会自动在容器上下文中查找,和自己对象 类型相同的 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="cat" class="com.xg.pojo.Cat"/>
<bean id="dogNew" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People" autowire="byType">
<property name="name" value="遇见星光"/>
</bean>
</beans>
小结:
- ByName 的时候,需要保证所有的bean 的 id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
- ByType 的时候,需要保证所有的bean 的 class唯一,并且这个bean需要和自动注入的类型一致!
1.4、注解实现自动装配
JDK1.5支持的注解,Spring2.5就支持注解了。
要使用注解须知
-
导入约束
-
配置注解支持
<?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/> </beans>
@Autowired
@Autowired 直接在属性上使用即可!也可以在Set方法上使用!
使用@Autowired 在属性上 可以不写 Set方法,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合byName!
如果使用@Autowired 自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、可以使用 @Qualifier(value="xxx")
配合 @Autowired
使用,指定一个唯一的bean对象注入!
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"
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.xg.pojo.Cat"/>
<bean id="cat2" class="com.xg.pojo.Cat"/>
<bean id="dog1" class="com.xg.pojo.Dog"/>
<bean id="dogNew" class="com.xg.pojo.Dog"/>
<bean id="people" class="com.xg.pojo.People"/>
</beans>
People
package com.xg.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class People {
// 如果显示定义了Autowired 的 required 属性为 false,说明这个对象可以为null,否则不能为空
@Autowired
private Cat cat;
@Autowired
@Qualifier("dog1")
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
}
小知识:
@Nullable // 字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
// 如果显示定义了Autowired 的 required 属性为 false,说明这个对象可以为null,否则不能为空
@Autowired(required = false)
private Cat cat;
@Resource
@Resource Java原生注解
@Resource(name = "cat2")
private Cat cat;
小结
@Autowired 和 @Resource 的区别
- 都是用来自动装配的,都可以放到字段上
- @Autowired 通过byType 的方式实现,而且必须要求对象存在
- @Resource 默认通过byName的方式实现,如果找不到名字,则通过byType实现!如果两个找不到的情况下就会报错
- 执行顺序不同:@Autowired 通过byType 的方式实现
分类:
spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人