Spring自动装配

所谓自动装配就是如果在beans.xml中,需要set值的对象的属性是对象,那么原来手动去ref这个值,变为通过ByNameByType的方式去set。

原始的手动装配

1
2
3
4
5
6
7
8
<bean id="cat" class="com.lrr.pojo.Cat"></bean>
<bean id="dog" class="com.lrr.pojo.Dog"></bean>

<bean id="people" class="com.lrr.pojo.Person">
<property name="name" value="p.K"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>

ByName自动装配

byname:会自动在容器上下文查找,和自己对象set方法后面的值对应的 beanid,需要保持ID唯一

1
2
3
4
5
<bean id="cat" class="com.lrr.pojo.Cat"></bean>
<bean id="dog" class="com.lrr.pojo.Dog"></bean>
<bean id="people2" class="com.lrr.pojo.Person" autowire="byName">
<property name="name" value="小佩华"/>
</bean>

ByType自动装配

byname:会自动在容器上下文查找,和自己对象属性相同 的bean

1
2
3
4
5
6
<!--ID可以不要--> 
<bean id="cat" class="com.lrr.pojo.Cat" scope="prototype"></bean>
<bean id="dog" class="com.lrr.pojo.Dog" scope="prototype"></bean>
<bean id="people3" class="com.lrr.pojo.Person" autowire="byType">
<property name="name" value="小佩华2"/>
</bean>

集中测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="cat" class="com.lrr.pojo.Cat" scope="prototype"></bean>
<bean id="dog" class="com.lrr.pojo.Dog" scope="prototype"></bean>


<bean id="people" class="com.lrr.pojo.Person">
<property name="name" value="p.K"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>


<!--自动装配 -->
<!--
byname:会自动在容器上下文查找,和自己对象set方法后面的值对应的 beanid
byname:会自动在容器上下文查找,和自己对象属性相同 的bean
-->
<bean id="people2" class="com.lrr.pojo.Person" autowire="byName">
<property name="name" value="小佩华"/>
</bean>
<bean id="people3" class="com.lrr.pojo.Person" autowire="byType">
<property name="name" value="小佩华2"/>
</bean>
</beans>

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import com.lrr.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* TODO
*
* @author DD
* @version 1.0
* @date 2021/7/6 22:30
*/
public class MyTest5 {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Person person = context.getBean("people", Person.class);
person.getDog().shot();
person.getCat().shot();
System.out.println(person.getName());
System.out.println("-----?");
Person person2 = context.getBean("people2", Person.class);
person2.getCat().shot();
person2.getDog().shot();
System.out.println(person2.getName());
System.out.println("-----?");
Person people3 = context.getBean("people3", Person.class);
people3.getDog().shot();
people3.getCat().shot();
System.out.println(people3.getName());
}

}

输出:

1
2
3
4
5
6
7
8
9
10
11
wang~
maio~
p.K
-----?
maio~
wang~
小佩华
-----?
wang~
maio~
小佩华2
作者

bd160jbgm

发布于

2021-07-07

更新于

2021-07-07

许可协议