Spring之Bean的注解自动装配 àì夳堔傛蜴生んèń 2023-01-08 08:25 111阅读 0赞 ### 前言 ### > 自动装配是Spring满足Bean依赖的一种方式。 > > XML自动装配比起注解自动装配还是比较繁琐的,所以学习一下更简便的注解自动装配。 > jdk1.5支持注解,spring2.5就支持注解了。 **注解自动装配须知:** 1. 导入约束 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 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> **直接上demo:** 猫类: public class Cat { public void shout(){ System.out.println("猫叫"); } } 狗类: public class Dog { public void shout(){ System.out.println("狗叫"); } } 人类:(一个人有一只猫和一只狗) 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 + '\'' + '}'; } } 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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="cat" class="cn.Cat"></bean> <bean id="dog" class="cn.Dog"></bean> <!--byname:会自动在容器上下文中查找,和自己对象set方法后面值对应的bean id --> <!--bytype:会自动在容器上下文中查找,和自己对象属性类型相同的bean --> <bean id="people" class="cn.People" autowire="byName"> <property name="name" value="詹大先生"></property> <property name="cat" ref="cat"></property> <property name="dog" ref="dog"></property> </bean> </beans> 测试类: public class MyTest { //主类 可以通过 psvm打出来 不用一个个代码去敲 public static void main(String[] args) { //获取Spring 的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people=context.getBean("people", People.class); people.getCat().shout(); people.getDog().shout(); } } 测试结果: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1NTAyMjA2_size_16_color_FFFFFF_t_70][] **注解自动装配后:** 人类:(直接在实体类的属性上加上@Autowired 即可。) public class People { //一个人有一只猫和一只狗 //注解自动装配 @Autowired private Cat cat; @Autowired 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 + '\'' + '}'; } } 注解自动装配 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="cn.Cat"></bean> <bean id="dog" class="cn.Dog"></bean> <bean id="people" class="cn.People"></bean> </beans> 测试结果: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1NTAyMjA2_size_16_color_FFFFFF_t_70][] ## ## ## ## ### @Resource ### 默认通过ByName的方式进行自动装配(就是根据id来自动装配)。 ### **@Autowired 和 @Qualifier** ### 假设我们要在beans.xml中再去添加一个id为dog666 类型为cn.Dog 的对象 <bean id="dog666" class="cn.Dog"></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" 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="cn.Cat"></bean> <bean id="dog" class="cn.Dog"></bean> <bean id="dog666" class="cn.Dog"></bean> <bean id="people" class="cn.People"></bean> </beans> 此时存在2个class为cn.Dog的对象,那么注解自动装配单单用使 @Autowired 是不够的。 我们就需要使用 @Autowired 和 @Qualifier搭配 //注解自动装配 //使用 @Autowired 和 @Qualifier搭配 @Autowired private Cat cat; @Autowired @Qualifier(value = "dog666") private Dog dog; **那么我们就可以知道,注解自动装配默认先按照ByType方式装配。(ByType方式:按照类型进行自动装配)** **看完如果对你有帮助,点赞支持!** ![20210119214632275.gif][] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1NTAyMjA2_size_16_color_FFFFFF_t_70]: /images/20221119/fe26d16d499646f5a8fd4207be121b51.png [20210119214632275.gif]: /images/20221119/a4ba2c1b3f2141ae954b936ac2d69436.png
还没有评论,来说两句吧...