装配(给对象中的属性赋值初始化)
自动装配具有以下优点:
-
自动装配可以大大减少指定属性或构造函数参数的需要。
-
随着对象的 Developing,自动装配可以更新配置。例如,如果您需要向类中添加一个依赖项,则无需修改配置即可自动满足该依赖项。因此,自动装配在开发过程中特别有用,而不必担心当代码库变得更稳定时切换到显式接线的选择。
在Spring中有三种装配的方式
- 在xml中显示的配置
- 在java中显示配置
- 隐式的自动装配bean
隐式的自动装配bean
Mode | Explanation |
---|---|
no | (默认)无自动装配。 Bean 引用必须通过ref 元素定义。对于大型部署,建议不要更改默认设置,因为明确指定协作者可以提供更好的控制和清晰度。在某种程度上,它记录了系统的结构。 |
byName | 按属性名称自动布线。 Spring 寻找与需要自动装配的属性同名的 bean。例如,如果将一个 bean 定义设置为按名称自动装配,并且包含一个* master 属性(即,它具有一个 setMaster(..)*方法),那么 Spring 将查找一个名为master 的 bean 定义,并使用它来设置属性。 |
byType | 如果容器中恰好存在一个该属性类型的 bean,则允许自动装配该属性。如果存在多个错误,则会引发致命异常,这表明您可能不对该 bean 使用* byType *自动装配。如果没有匹配的 bean,则什么也没有发生。该属性未设置。 |
constructor | 与* byType *类似,但适用于构造函数参数。如果容器中不存在构造函数参数类型的一个 bean,则将引发致命错误。 |
pojo
import lombok.Data;
class Cat {
public void shout(){
System.out.println("喵~");
}
}
class Dog {
public void shout(){
System.out.println("汪~");
}
}
@Data
public class Human {
private Dog dog;
private Cat cat;
private String name;
}
默认(no)
<bean id="dog" class="com.spring.pojo.Dog"/>
<bean id="cat" class="com.spring.pojo.Cat"/>
<bean id="human" class="com.spring.pojo.Human">
<property name="name" value="kitty guy"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
byName
<bean class="com.spring.pojo.Dog"/>
<bean class="com.spring.pojo.Cat"/>
<bean id="human" class="com.spring.pojo.Human" autowire="byType">
<property name="name" value="kitty guy"/>
</bean>
byType
<bean id="dog" class="com.spring.pojo.Dog"/>
<bean id="cat" class="com.spring.pojo.Cat"/>
<bean id="human1" class="com.spring.pojo.Human" autowire="byName">
<property name="name" value="kitty guy"/>
</bean>
自动装配的局限性和缺点
当在项目中一致使用自动装配时,自动装配效果最佳。如果通常不使用自动装配,那么使用开发人员仅连接一个或两个 bean 定义可能会使开发人员感到困惑。
注解自动装配
步骤
1、 在bean.xml中注册context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/><!--不可遗忘-->
</beans>
2、在pojo中使用注解
- @Autowired
类似byType
- @Resource(name = "")
没有name参数就类似byType,name参数可以指定配置文件中的id。适用同一个对象有多个id 。 是@Qualifier(value = "")和@Autowired的集合体
@Autowired样例
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="com.spring.pojo.Cat"/>
<bean class="com.spring.pojo.Dog"/>
<bean id="human" class="com.spring.pojo.Human">
<property name="name" value="kitty"/>
</bean>
</beans>
使用注解
@Data
public class Human {
@Autowired
private Dog dog;
@Autowired
private Cat cat;
private String name;
}
@Resource(name = "")样例
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="com.spring.pojo.Cat"/>
<bean class="com.spring.pojo.Dog"/>
<bean id="cat001" class="com.spring.pojo.Cat"/>
<bean id="dog001" class="com.spring.pojo.Dog"/>
<bean id="human" class="com.spring.pojo.Human">
<property name="name" value="kitty"/>
</bean>
</beans>
使用注解
@Data
public class Human {
@Resource(name = "dog001")
private Dog dog;
@Resource(name = "cat001")
private Cat cat;
private String name;
}
注解自动装配要点和小扩展
@Autowired(required = false)
表示可以为null和@Nullable
类似