2022.5.28 Bean的自动装配与注解开发

7、Bean的自动装配

  • 自动装配是Spring满足bean依赖一种方式!

  • Spring会在上下文中自动寻找,并自动给bean装配属性!

在Spring中有三种装配的方式

  • 在xml中显示的配置

  • 在java中显示配置

  • 隐式的自动装配bean(重要)

7.1、环境搭建

 

 

cat

1 package com.xing.pojo;
2 
3 public class Cat {
4     public void shout() {
5         System.out.println("miao~");
6     }
7 }

 

dog

1 package com.xing.pojo;
2 
3 public class Dog {
4     public void shout() {
5         System.out.println("wang~");
6     }
7 }

 

person

 1 package com.xing.pojo;
 2 
 3 public class Person {
 4     private Cat cat;
 5     private Dog dog;
 6     private String name;
 7 
 8     public Cat getCat() {
 9         return cat;
10     }
11 
12     public void setCat(Cat cat) {
13         this.cat = cat;
14     }
15 
16     public Dog getDog() {
17         return dog;
18     }
19 
20     public void setDog(Dog dog) {
21         this.dog = dog;
22     }
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     @Override
33     public String toString() {
34         return "Person{" +
35                 "cat=" + cat +
36                 ", dog=" + dog +
37                 ", name='" + name + '\'' +
38                 '}';
39     }
40 }

 

beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="cat" class="com.xing.pojo.Cat"/>
 8     <bean id="dog" class="com.xing.pojo.Dog"/>
 9 
10     <bean id="person" class="com.xing.pojo.Person">
11         <property name="name" value="小明"/>
12         <property name="cat" ref="cat"/>
13         <property name="dog" ref="dog"/>
14     </bean>
15 </beans>

 

test

 1 import com.xing.pojo.Person;
 2 import org.junit.Test;
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class MyTest {
 7 
 8     @Test
 9     public void test1() {
10         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
11         Person p = context.getBean("person", Person.class);
12         p.getCat().shout();
13         p.getDog().shout();
14 
15 
16     }
17 }

 

 

 

7.2、byName

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="cat" class="com.xing.pojo.Cat"/>
 8     <bean id="dog" class="com.xing.pojo.Dog"/>
 9 
10     <!--
11     byName:会自动在容器上下文中配置和自己对象set方法后面的值(例如,setCat 会找cat)对应的 bean的id 
12     这样就不用配置 <property name="dog" ref="dog"/>
13     如果<property name="dog111" ref="dog"/>就不能自动配置
14     -->
15     <bean id="person" class="com.xing.pojo.Person" autowire="byName">
16         <property name="name" value="小明"/>
17 <!--        <property name="cat" ref="cat"/>-->
18 <!--        <property name="dog" ref="dog"/>-->
19     </bean>
20 </beans>

 

test综上

7.3、byType

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="cat" class="com.xing.pojo.Cat"/>
 8     <bean id="dog111" class="com.xing.pojo.Dog"/>
 9     <!--
10     byType:会自动在容器上下文中查找,和有己对象属性类型相同的bean !
11     但是不能有两个相同类型的bean
12     -->
13     <bean id="person" class="com.xing.pojo.Person" autowire="byType">
14         <property name="name" value="小明"/>
15 <!--        <property name="cat" ref="cat"/>-->
16 <!--        <property name="dog" ref="dog"/>-->
17     </bean>
18 </beans>

 

test综上

小结:

  • byname的时候,需要保证所有bean的id唯一

  • bytype的时候,需要保证所有bean的class唯一

7.4、使用注解实现自动装配

要使用注解须知:

1.在xml导入约束

xmlns:context="http://www.springframework.org/schema/context"

2.配置注解的支持 <context:annotation-config/>

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         https://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <context:annotation-config/>
11 
12 </beans>

@Autowired(先按类型再按名字 自动装配bean)

@Autowired按照byType 注入,再按byName自动注入,两个都不符合报错

名字不符合且类型相同,无法装配
1 <bean id="cat2" class="com.xing.pojo.Cat"/>
2 <bean id="cat111" class="com.xing.pojo.Cat"/>

直接在属性上使用即可!也可以在set方式上使用!

使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC (Spring)容器中存在,且名合名字byname!

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         https://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <bean id="cat" class="com.xing.pojo.Cat"/>
11     <bean id="cat111" class="com.xing.pojo.Cat"/>
12     <bean id="dog111" class="com.xing.pojo.Dog"/>
13     <bean id="person" class="com.xing.pojo.Person"/>
14 
15     <context:annotation-config/>
16 </beans>

 

person

 1 package com.xing.pojo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 
 5 public class Person {
 6     //如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为生
 7     @Autowired(required = false)
 8     private Cat cat;
 9     @Autowired
10     private Dog dog;
11     private String name;
12 
13     public Cat getCat() {
14         return cat;
15     }
16 
17     public Dog getDog() {
18         return dog;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     @Override
26     public String toString() {
27         return "Person{" +
28                 "cat=" + cat +
29                 ", dog=" + dog +
30                 ", name='" + name + '\'' +
31                 '}';
32     }
33 }

 

test综上

存在相同类型的bean

 1 package com.xing.pojo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.beans.factory.annotation.Qualifier;
 5 
 6 public class Person {
 7     @Autowired
 8     private Cat cat;
 9     @Autowired
10     @Qualifier(value = "dog111")  //指定要运行的bean
11     private Dog dog;
12     private String name;
13 
14     public Cat getCat() {
15         return cat;
16     }
17 
18     public Dog getDog() {
19         return dog;
20     }
21 
22     public String getName() {
23         return name;
24     }
25 
26     @Override
27     public String toString() {
28         return "Person{" +
29                 "cat=" + cat +
30                 ", dog=" + dog +
31                 ", name='" + name + '\'' +
32                 '}';
33     }
34 }

 

beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         https://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <bean id="cat" class="com.xing.pojo.Cat"/>
11     <bean id="dog" class="com.xing.pojo.Dog"/>
12     <bean id="dog111" class="com.xing.pojo.Dog"/>
13     <bean id="person" class="com.xing.pojo.Person"/>
14 
15     <context:annotation-config/>
16 </beans>

 

8、使用注解开发

使用注解需要导入context约束,增加注解的支持!

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         https://www.springframework.org/schema/context/spring-context.xsd">
 9 
10 
11     <context:annotation-config/>
12 </beans>

 

@Component(替换bean)

组件,放在类上,说明这个类被Spring管理了,就是bean !

@component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

  • dao 【@Repository】

  • service 【@service】

  • controller 【@controller)

这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean

 

 

User

1 package com.xing.pojo;
2 
3 import org.springframework.stereotype.Component;
4 
5 @Component  //等价于<bean id="user" class="com.xing.pojo.User"/>
6 public class User {
7     public String name = "小明";
8 }

 

Userdao

1 package com.xing.dao;
2 
3 import org.springframework.stereotype.Repository;
4 
5 @Repository
6 public class Userdao {
7 }

 

UserService

1 package com.xing.service;
2 
3 import org.springframework.stereotype.Service;
4 
5 @Service
6 public class UserService {
7 }

UserController

1 package com.xing.controller;
2 
3 import org.springframework.stereotype.Controller;
4 
5 @Controller
6 public class UserController {
7 }

applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         https://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         https://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <!--指定要扫描的包,这个包下的注解就会生效-->
11     <context:component-scan base-package="com.xing"/>
12     <context:annotation-config/>
13 
14 
15 </beans>

 

test

 1 import com.xing.pojo.User;
 2 import org.junit.Test;
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 public class MyTest {
 7 
 8     @Test
 9     public void test1() {
10         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
11         User user = context.getBean("user", User.class);
12         System.out.println(user.name);
13 
14     }
15 }

 

 

 

@Value(用于属性赋值)

User

 1 package com.xing.pojo;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 
 7 @Component  //等价于<bean id="user" class="com.xing.pojo.User"/>
 8 public class User {
 9     //相当<property name="name" value="小明"/>
10     @Value("小明") //给属性赋值
11     public String name;
12 }

 

法二:

 1 package com.xing.pojo;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 
 7 @Component  //等价于<bean id="user" class="com.xing.pojo.User"/>
 8 public class User {
 9 
10     public String name;
11     
12     //相当<property name="name" value="小明"/>
13     @Value("小明")
14     public void setName(String name) {
15         this.name = name;
16     }
17 }

 

小结

xml与注解:

  • xml更加万能,适用于任何场合!维护简单方便。

  • 注解不是自己类使用不了,维护相对复杂!

xml与注解最佳实践:

  • xml 用来管理bean;

  • 注解只负责完成属性的注入;,

 

posted @ 2022-06-06 17:31  暴躁C语言  阅读(23)  评论(0编辑  收藏  举报