Bean的自动装配

Bean的自动装配

1. 简介

自动装配是Spring满足Bean依赖的一种方式!

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

在Spring中有三种装配的方式

  1. 在xml中显式的配置

  2. 在java中显式的装配

  3. 隐式的自动装配Bean [重要]

2. 测试

1. 环境搭建

一个人有两个宠物

package com.wang.pojo;

public class Cat {
    public void shout() {
        System.out.println("miao ~");
    }
}
package com.wang.pojo;

public class Dog {
    public void shout() {
        System.out.println("wang ~");
    }
}
package com.wang.pojo;

public class People {

    private String name;
    private Cat cat;
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = 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;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", cat=" + cat +
                ", 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.wang.pojo.Cat"/>
    <bean id="dog" class="com.wang.pojo.Dog"/>

    <bean id="people" class="com.wang.pojo.People">
        <property name="name" value="wang sky"/>
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
    </bean>

</beans>

2. ByName与ByType自动装配

<?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="com.wang.pojo.Cat"/>
    <bean id="dog" class="com.wang.pojo.Dog"/>

    <!--
    byName: 会自动在容器上下文中查找, 和自己对象set方法后面的值对应的beanId
    byType: 会自动在容器上下文中查找, 和自己对象属性类型相同的bean
    -->
    <bean id="people" class="com.wang.pojo.People" autowire="byType">
        <property name="name" value="wang sky"/>
    </bean>

</beans>

3. 小结

  • byName的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的名称一致
  • byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致

4. 使用注解实现自动装配

要使用注解须知:

1. 导入约束

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"

2. 配置注解的支持

<context:annotation-config/>

3. @Autowired

  • 直接在属性上使用即可!

  • 也可以在setter方式上使用!

  • 使用Autowired我们可以不用编写Setter方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合类型byType!

  • @Nullable标记的属性,说明这个属性可以为null(可以不给传参数)

  • @Autowired(required = false) : 如果显式的定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空

  • 如果@Autowired自动装配的环境比较复杂,无法通过一个@Autowired注解实现自动装配的时候,我们可以使用@Qualifier(value = "xxx")去配置(value为id的值)@Autowired的使用,指定唯一的bean对象注入!

  • package com.wang.pojo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    public class People {
    
        private String name;
        @Autowired
        private Cat cat;
        @Autowired
        @Qualifier(value = "dog222")
        private Dog dog;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = 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;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "name='" + name + '\'' +
                    ", cat=" + cat +
                    ", dog=" + dog +
                    '}';
        }
    }
    

3. @Resource注解

属于javax.annotation包,与自动装配类似

jdk1.8以上是不支持@Resource注解的

解决方法:

在使用Spring注解开发中,使用@Resource报空指针异常时有两个解决方案:

  1. 使用jdk8

  2. 在Maven中导入如下依赖

    <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
    </dependency>
    

4. 小结

@Resource与@Autowired的区别

  • @Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。@Autowired 通过byType的方式实现

  • @Autowired默认按类型装配(这个注解是属于Spring的),默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置它的required

属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用

  • @Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上

    时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但

    是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。(先byName 再byType)

posted @ 2020-09-03 14:35  山人西来  阅读(277)  评论(0编辑  收藏  举报