首页 > 其他分享 >Spring(十):注解实现自动装配

Spring(十):注解实现自动装配

时间:2022-09-26 10:44:07浏览次数:47  
标签:装配 name Autowired Spring private cat Bean 注解 id

上一篇文章我们已经学习了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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean class="com.jms.pojo.Dog"/>
    <bean class="com.jms.pojo.Cat"/>

    <bean id="people" class="com.jms.pojo.People">
        <property name="name" value="jms"/>
    </bean>

</beans>

1.@Autowired

@Autowired注解

@Autowired注解是spring的一个注解,这个注解可以在属性、构造函数、set方法等地方使用,能够实现Bean的自动装配。

(1)在属性上使用

public class People {
    private String name;
    @Autowired
    private Dog dog;
    @Autowired
    private Cat cat;
}

(2)在构造函数上使用

public class People {
    private String name;

    private Dog dog;

    private Cat cat;

    @Autowired
    public People(Dog dog, Cat cat) {
        this.dog = dog;
        this.cat = cat;
    }

(3)在set方法上使用

public class People {
    private String name;

    private Dog dog;

    private Cat cat;

    @Autowired
    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Autowired
    public void setCat(Cat cat) {
        this.cat = cat;
    }
}

这三种方法进行测试得到的都可以获得最后的结果,测试如下:

 @Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        People people = applicationContext.getBean("people", People.class);
        System.out.println(people.getName());
        people.getDog().bark();
        people.getCat().bark();
    }

测试结果:

 

 

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

 

 

public class People {
    private String name;

    @Autowired
    private Dog dog;

    @Autowired(required = false)
    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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean class="com.jms.pojo.Dog"/>

    <bean id="people" class="com.jms.pojo.People">
        <property name="name" value="jms"/>
    </bean>

</beans>

测试:

@Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        People people = applicationContext.getBean("people", People.class);
        System.out.println(people.getName());
        people.getDog().bark();
        System.out.println(people.getCat());
    }

测试结果:

 

 

 从测试结果可以看到,cat变成了null。

此时,我们了解了@Autowired的基本用法,我们来思考一个问题,如果有多个同类型的bean会发生什么呢?此时我们修改Cat类型的bean为两个:

    <bean id="cat2" class="com.jms.pojo.Cat"/>
    <bean id="cat1" class="com.jms.pojo.Cat"/>

 

结果报错了,它会让我们去选择bean。上面很明显两个bean的id都和属性名不同,如果让其中一个id与属性名相同能否找到呢?

    <bean id="cat" class="com.jms.pojo.Cat"/>
    <bean id="cat1" class="com.jms.pojo.Cat"/>

不再报错。

如果属性名与字段名不同,能否将上面的问题解决呢?答案是可以的,我们需要用到@Qualifier(value = ""),我们只需要让value等于bean的id即可。

如下面的示例:

   <bean id="cat" class="com.jms.pojo.Cat"/>
    <bean id="cat1" class="com.jms.pojo.Cat"/>
  @Autowired
    @Qualifier(value = "cat1")
    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
    private Dog dog;
<bean class="com.jms.pojo.Dog"/>

没有问题。即使有id且id随意,也是能够找到Bean的,因为类型匹配且唯一。

(2)不使用name属性,Bean不唯一但有能够匹配属性名的id

   <bean id="dog" class="com.jms.pojo.Dog"/>
    <bean id="dog1" class="com.jms.pojo.Dog"/>

这种情况也没有问题,因为id能够与属性名匹配。

(3)使用name属性,Bean不唯一但有能匹配name的id

@Resource(name = "dog1")
    private Dog dog;
   <bean id="dog" class="com.jms.pojo.Dog"/>
    <bean id="dog1" class="com.jms.pojo.Dog"/>

没有问题,因为name能与Bean的id匹配。

总而言之,@Resource与@Autowried用法基本相同,都是先去容器中匹配类型,如过有多个相同类型则将属性名与id进行匹配;当使用了name或者@Qualifier(value = "")时,会直接去对应类型的Bean中找id能与name或value值相匹配的,如果找不到,即便类型匹配且唯一也会报错。

我们用一个图来更加明了的看一下整个流程(以@Autowried为例):

 

 

 

(本文仅作个人学习记录用,如有纰漏敬请指正)

 

标签:装配,name,Autowired,Spring,private,cat,Bean,注解,id
From: https://www.cnblogs.com/jmsstudy/p/16727509.html

相关文章