什么是Spring Bean的自动装配?
- 自动装配是Spring满足bean依赖一种方式
- Spring会在上下文种自动寻找,并自动给bean装配属性
Spring种有三种装配方式
1.在xml种显示配置
2.隐式的自动装配bean【重要】
3.在java种显示配置
1.在xml种显示配置
1.1 创建pojo
/**
* @author LongXi
* @create 2021-05-28 20:24
*/
public class Dog {
public void shout(){
System.out.println("汪汪");
}
}
/**
* @author LongXi
* @create 2021-05-28 20:24
*/
public class Cat {
public void shout(){
System.out.println("喵喵~");
}
}
/**
* @author LongXi
* @create 2021-05-28 20:26
*/
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;
}
}
人有2个宠物,狗和猫,都会叫。
bean.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.lx.pojo.Cat"/>
<bean id="dog" class="com.lx.pojo.Dog"/>
<bean id="people" class="com.lx.pojo.People">
<property name="dog" ref="dog"/>
<property name="cat" ref="cat"/>
<property name="name" value="龙兮"/>
</bean>
</beans>
测试类
/**
* @author LongXi
* @create 2021-05-28 20:32
*/
public class MyTest {
@Test
public void Test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
}
结果
下面我们改一下配置文件
<?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.lx.pojo.Cat"/>
<bean id="dog" class="com.lx.pojo.Dog"/>
<bean id="people" class="com.lx.pojo.People" autowire="byName">
<property name="name" value="龙兮"/>
</bean>
</beans
测试
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.lx.pojo.Cat"/>
<bean id="dog11" class="com.lx.pojo.Dog"/>
<bean id="people" class="com.lx.pojo.People" autowire="byType">
<property name="name" value="龙兮"/>
</bean>
</beans>
测试
byType:会自动在容器上下文种查找和自己对象属性类型相同的Bean。
保证类型全局唯一才会装配,否则报错。
并且bean的id可以省略
<bean class="com.lx.pojo.Cat"/>
<bean class="com.lx.pojo.Dog"/>
<bean id="people" class="com.lx.pojo.People" autowire="byType">
<property name="name" value="龙兮"/>
</bean>
这样也是可以的
总结:
byName:需要保证bean的id全局唯一,并且这个bean需要和自动注入属性的set方法的值一致。
byType:需要保证bean的Class全局唯一,并且这个bean需要和自动注入属性的类型一致!
2.使用注解实现自动装配
1.导入约束 :context
2.配置注解的支持: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">
<bean id="cat" class="com.lx.pojo.Cat"/>
<bean id="dog" class="com.lx.pojo.Dog"/>
<bean id="people" class="com.lx.pojo.People"/>
<context:annotation-config/>
</beans>
实体类
/**
* @author LongXi
* @create 2021-05-28 20:26
*/
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
测试:
@Autowired
1.直接在对象的属性上使用即可,也可以在set方法上使用。
2.使用Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(spring)容器种存在,且符合名字byName。
附加:
@Nullable 字段加了这个注解,说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
************************************************
@Autowired(required = false)
private Cat cat;
如果对象种属性显示定义了required = false,说明这个属性可以null,否则不允许为空。
如果@Autowired自动装配环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,
我们可以使用@Qualified(value=xxx)去配和@Autowired,指定一个唯一的bean对象注入。
<bean id="dog" class="com.lx.pojo.Dog"/>
<bean id="dog22" class="com.lx.pojo.Dog"/>
@Autowired
@Qualifier(value = "dog22")
private Dog dog;
@Resource:java的原生注解
<bean id="cat" class="com.lx.pojo.Cat"/>
<bean id="cat2" class="com.lx.pojo.Cat"/>
**********************************************
@Resource(name = "cat2")
private Cat cat;
先按名称装配,再按类型装配,可以有多个类型一样的不同name,可以指定唯一name
总结:@Resource和@Autowired区别
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired用过byType的方式实现,如果找不到类型,则通过byName实现。而且必须要求这个对象存在。【常用】
- @Resource默认通过byName的方式实现,如果找不到名称,则通过byType实现!
- 执行顺序不同:@Autowired用过byType的方式实现,@Resource默认通过byName的方式实现
使用注解开发
在Spring4之后,要使用注解开发,必须要保证aop的包导入了
使用注解需要导入context约束,增加注解的支持。
1.bean
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:component-scan base-package="com.lx.pojo"/>
<context:annotation-config/>
</beans>
@Component
public class User {
@Value("龙兮")
public String name;
//@Value("龙兮")
public String getName() {
return name;
}
}
3.衍生的注解
@Compoent 有几个衍生注解,我们在web开发种,会按照mvc三层架构分层!
- dao 【@Repository】
- service【@Service】
- controller【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring容器中,装配bean。
4.自动装配的注解
@Autowired
@Resource
5.作用域
/**
* @author LongXi
* @create 2021-05-28 22:45
*/
@Controller
@Scope("prototype")
public class UserController {
}
6.总结:
xml:更加万能,使用于任何场合,维护简单方便。
注解:不是自己的类使用不了,维护相对复杂。
最佳实践:
xml用来管理bean
注解只负责完成属性的注入。
我们在使用的过程中,只需要注意一个问题:必须让注解生效 ,需要开启注解的支持。
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.lx"/>
<context:annotation-config/>
3.使用Java的方式配置Spring
我们现在完全不使用Spring的xml配置了,全权交给java来做。
JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能。
实体类:
/**
* @author LongXi
* @create 2021-05-30 9:07
*/
//这个注解的意思,就是说明这个类配托管了
@Component
public class User {
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
@Value("龙兮")
public void setName(String name) {
this.name = name;
}
}
配置类
/**
* @author LongXi
* @create 2021-05-30 9:09
*/
//这个也会被Spring容器过关,注册到容器中,因为它本来就是一个@Compoent
//@Configuration 代表这是一个配置类,就和我们之前看的beans。xml是一样的
@Configuration
@ComponentScan("com.lx.pojo")
@Import(MyConfig2.class)
public class MyConfig {
//注册一个bean,就相当于我们之前写的Bean标签
//这个方法的名字,就相当于bean标签的ID属性
//这个方法的放回置,就相当于bean标签的class属性
//
@Bean
public User getUser(){
return new User();//就是放回要注入的bean对象
}
}
测试类
/**
* @author LongXi
* @create 2021-05-30 9:11
*/
public class MyTest {
@Test
public void MyTest1(){
//如果完全使用了配置类方式,我们只能通过AnnotationConfigApplicationContext上下文来获取容器,通过配置的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User user = context.getBean("getUser", User.class);
System.out.println(user.getName());
}
}
这中纯java的配置方式,在SpringBoot中随处可见!