上一篇文章我们已经学习了Bean的自动装配,是在xml文件中配置autowire来实现的,现在我们来学习一下通过注解来实现自动装配。
一、使用注解需要的准备工作
使用注解在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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码 二、注解的使用
仍然是上一篇文章的三个类,People、Cat、Dog。
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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码 1.@Autowired
@Autowired注解
@Autowired注解是spring的一个注解,这个注解可以在属性、构造函数、set方法等地方使用,能够实现Bean的自动装配。
(1)在属性上使用- public class People {<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private String name;<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>@Autowired<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Dog dog;<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>@Autowired<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Cat cat;}
复制代码 (2)在构造函数上使用(3)在set方法上使用这三种方法进行测试得到的都可以获得最后的结果,测试如下:测试结果:

除此之外@Autowired还有一个属性required默认为true,我们可以设置它为false,当我们设置了false时,被注解的值就可以为null,否则不行。

- public class People {<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private String name;<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>@Autowired<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Dog dog;<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>@Autowired(required = false)<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Cat cat;}
复制代码 删掉xml文件中cat的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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码 测试:测试结果:

从测试结果可以看到,cat变成了null。
此时,我们了解了@Autowired的基本用法,我们来思考一个问题,如果有多个同类型的bean会发生什么呢?此时我们修改Cat类型的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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码
结果报错了,它会让我们去选择bean。上面很明显两个bean的id都和属性名不同,如果让其中一个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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans><?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码 不再报错。
如果属性名与字段名不同,能否将上面的问题解决呢?答案是可以的,我们需要用到@Qualifier(value = ""),我们只需要让value等于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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码- @Autowired<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>@Qualifier(value = "cat1")<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Cat cat;
复制代码 总结一下:
@Autowired注解是先去IOC容器中查到对应类型的Bean,如果有多个对应类型的Bean,然后根据Bean的id进行匹配。
@Autowired有一个属性required默认为true,我们可以设置它为false。true意味着我们必须找到Bean,false意味着可以找不到Bean(null)。
@Autowired可以@Qualifier(value = "")配合使用,这样就会去容器中找id与value值匹配的Bean。并且此时即便只有一个相同类型的Bean,如果id不匹配也会报错。
2.@Resource注解
@Resource注解是java中的一个注解,使用它同样可以实现Bean的自动装配。
@Resource有一个name属性,当使用name属性时,会根据name的值匹配Bean的id。
下面是使用的几个例子:
(1)不使用name属性,唯一Bean没有id- @Resource<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Dog dog;
复制代码- [/code]没有问题。即使有id且id随意,也是能够找到Bean的,因为类型匹配且唯一。
- (2)不使用name属性,Bean不唯一但有能够匹配属性名的id
- [code] <?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码 这种情况也没有问题,因为id能够与属性名匹配。
(3)使用name属性,Bean不唯一但有能匹配name的id- @Resource(name = "dog1")<?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>private Dog dog;
复制代码- <?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
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
- <bean id="cat2" class="com.jms.pojo.Cat"/>
- <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
- <context:annotation-config/>
- </beans>
复制代码 没有问题,因为name能与Bean的id匹配。
总而言之,@Resource与@Autowried用法基本相同,都是先去容器中匹配类型,如过有多个相同类型则将属性名与id进行匹配;当使用了name或者@Qualifier(value = "")时,会直接去对应类型的Bean中找id能与name或value值相匹配的,如果找不到,即便类型匹配且唯一也会报错。
我们用一个图来更加明了的看一下整个流程(以@Autowried为例):

(本文仅作个人学习记录用,如有纰漏敬请指正)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |