Bean的自动装配和注解实现自动装配

107 阅读2分钟

Bean的自动装配

  1. 什么是自动装配:
  • 自动装配是使用spring满足bean依赖的一种方法
  • spring会在应用上下文中为某个bean寻找其依赖的bean。
  1. 自动装配的三种装配机制:在xml中显式配置在java中显式配置隐式的自动装配bean

  2. 来个例子瞧瞧:

创建三个实体类,Dog,Cat,User

package pojo;

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

package pojo;

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

package pojo;

public class User {
    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;
    }
}

编写Spring配置文件beans.xml

  1. 在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="pojo.Cat"/>
    <bean id="dog" class="pojo.Dog"/>

    <bean id="user" class="pojo.User">
         <property name="dog" ref="dog"/>
         <property name="cat" ref="cat"/>
         <property name="name" value="xiaomi"/>
     </bean>
     
</beans>

测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.User;

public class DemoTest {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = context.getBean("user", User.class);
        user.getCat().shout();
        user.getDog().shout();
        System.out.println(user.getName());
    }


}

  1. 隐式的自动装配bean:通过autowrite属性实现

autowrite byName

<?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="pojo.Cat"/>
    <bean id="dog" class="pojo.Dog"/>
    
    <!-- 使用byName自动装配   -->
    <bean id="user" class="pojo.User" autowire="byName">
        <property name="name" value="xiaomi"/>
    </bean>
</beans>

autowrite 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="pojo.Cat"/>
    <bean id="dog" class="pojo.Dog"/>
    
    <!-- 使用byType自动装配   -->
    <bean id="user" class="pojo.User" autowire="byType">
        <property name="name" value="xiaomi"/>
    </bean>
</beans>

结果:

image.png

注意点:

image.png

image.png

通过这两个截图可以发现,通过ByName方式进行自动装配,它会自动去查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。如果有,就取出注入;如果没有,就报空指针异常。

image.png

image.png

image.png

通过上面三个截图,可以发现,通过ByType方式实现自动装配,id属性可写可不写,但是如果多个id对应同一个类,这是不行的,会报错。ByType就是通过class="XXX"中的类型去自动装配的。

注解实现自动装配

注解实现自动装配的步骤:

  1. 在spring配置文件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">

</beans>
  1. 开启属性注解支持
<context:annotation-config/>
  1. 配置基础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="dog" class="pojo.Dog"/>
    <bean id="cat" class="pojo.Cat"/>
    <bean id="user" class="pojo.User"/>

</beans>
  1. 在实体类通过@Autowired注解实现自动装配

image.png

  1. 在实体类通过@Resource注解实现自动装配

image.png

注意点:

  1. @Autowired先通过ByType方式装配,其次通过ByName,如果添加@Qualifier(value = "dog1"),则可以直接通过ByName方式装配
  2. @Resource先通过ByName方式装配,其次通过ByType
  3. @Qualifier不能单独使用,要和@Autowired搭配使用。 image.png