【Spring Framework】7、自动装配

1、自动装配

  • 自动装配是Spring满足bean以来的一种方式!
  • Spring会在上下文中自动寻找,并自动给bean 配置属性!

在Spring中有三种装配的方式:

  1. 在xml中显示的配置
  2. 在Java中显示配置
  3. 隐式的自动装配bean【重要】

1.1、测试环境搭建

环境搭建 : 一个人有两个宠物

People

package com.xg.pojo;

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 + '\'' +
                '}';
    }
}

Cat

package com.xg.pojo;

public class Cat {
    public void shout(){
        System.out.println("喵喵喵!");
    }
}

Dog

package com.xg.pojo;

public class Dog {
    public void shout(){
        System.out.println("汪汪汪!");
    }
}

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="cat" class="com.xg.pojo.Cat"/>
    <bean id="dog" class="com.xg.pojo.Dog"/>

    <bean id="people" class="com.xg.pojo.People">
        <property name="name" value="遇见星光"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>
</beans>

测试类

import com.xg.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);

        people.getCat().shout();
        people.getDog().shout();
    }
}

image

1.2、ByName自动装配

byName : 会自动在容器上下文中查找,和自己对象 set方法 后面的值对应的 bean id !
如果没有找到将会报错!

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.xg.pojo.Cat"/>
    <bean id="dog" class="com.xg.pojo.Dog"/>
    
    <bean id="people" class="com.xg.pojo.People" autowire="byName">
        <property name="name" value="遇见星光"/>
    </bean>
</beans>

1.3、ByType自动装配

byName : 会自动在容器上下文中查找,和自己对象 类型相同的 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.xg.pojo.Cat"/>
    <bean id="dogNew" class="com.xg.pojo.Dog"/>

    <bean id="people" class="com.xg.pojo.People" autowire="byType">
        <property name="name" value="遇见星光"/>
    </bean>
</beans>

小结:

  • ByName 的时候,需要保证所有的bean 的 id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
  • ByType 的时候,需要保证所有的bean 的 class唯一,并且这个bean需要和自动注入的类型一致!

1.4、注解实现自动装配

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>
    

@Autowired

@Autowired 直接在属性上使用即可!也可以在Set方法上使用!

使用@Autowired 在属性上 可以不写 Set方法,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合byName!

如果使用@Autowired 自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、可以使用 @Qualifier(value="xxx") 配合 @Autowired 使用,指定一个唯一的bean对象注入!

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="com.xg.pojo.Cat"/>
    <bean id="cat2" class="com.xg.pojo.Cat"/>
    <bean id="dog1" class="com.xg.pojo.Dog"/>
    <bean id="dogNew" class="com.xg.pojo.Dog"/>

    <bean id="people" class="com.xg.pojo.People"/>
</beans>

People

package com.xg.pojo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class People {
    // 如果显示定义了Autowired 的 required 属性为 false,说明这个对象可以为null,否则不能为空
    @Autowired
    private Cat cat;

    @Autowired
    @Qualifier("dog1")
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }
    
    public Dog getDog() {
        return dog;
    }
    
    public String getName() {
        return name;
    }
}

小知识:

@Nullable // 字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
    boolean required() default true;
}
// 如果显示定义了Autowired 的 required 属性为 false,说明这个对象可以为null,否则不能为空
@Autowired(required = false)
private Cat cat;

@Resource

@Resource Java原生注解

@Resource(name = "cat2")
private Cat cat;

小结

@Autowired 和 @Resource 的区别

  • 都是用来自动装配的,都可以放到字段上
  • @Autowired 通过byType 的方式实现,而且必须要求对象存在
  • @Resource 默认通过byName的方式实现,如果找不到名字,则通过byType实现!如果两个找不到的情况下就会报错
  • 执行顺序不同:@Autowired 通过byType 的方式实现
posted @ 2021-07-09 09:11  遇见星光  阅读(38)  评论(0编辑  收藏  举报