bean的自动装配

本文详细介绍了Spring中的自动装配机制,包括组件扫描和自动装配的概念,以及通过XML配置实现的ByName和ByType两种自动装配方式。此外,还讲解了如何使用注解实现自动装配,包括@Autowired和@Resource的用法及其区别。最后,通过示例代码展示了如何在实际应用中配置和使用这些装配方式。
摘要由CSDN通过智能技术生成

 1、前提

自动装配:

  • 自动装配是使用spring满足bean依赖的一种方法

  • spring会在应用上下文中为某个bean寻找其依赖的bean

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

  1. 在xml中显式配置
  2. 在java中显式配置
  3. 隐式的bean发现机制和自动装配【重要】

Spring的自动装配需要从两个角度来实现,或者说是两个操作:

  1. 组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean

  2. 自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI


2、测试

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

(1)新建一个项目

(2)新建两个实体类,Cat和Dog 都有一个叫的方法

Cat.java

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

Dog.java  

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

(3)新建一个用户类 People  

People.java

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Dog getDog() {
        return dog;
    }

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

    public Cat getCat() {
        return cat;
    }

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

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", dog=" + dog +
                ", cat=" + cat +
                '}';
    }
}

(4)编写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"   
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">



    <bean id="cat" class="com.gt.pojo.Cat"/>
    <bean id="dog123" class="com.gt.pojo.Dog"/>

    <bean id="people" class="com.gt.pojo.People"/>
        <property name="name" value="山姆你好呀"/>
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
    </beans>
</beans>

MyTest

import com.gt.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.getDog().shout();
        people.getCat().shout();
    }
}


3、ByName自动装配

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

    <!--<bean id="people" class="com.gt.pojo.People">
        <property name="name" value="山姆你好呀"/>
        <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
    </bean>-->

    <!--
     byName:会自动在容器上下本中查找,和自己对象set方法后面的值对应的beanId 如setDog 中的dog对应的beanId
     setDog setdog 都可以找到
     setDOG 就找不到了
     -->

    <bean id="people" class="com.gt.pojo.People" autowire="byName">
        <property name="name" value="山姆你好呀"/>
    </bean>


4、ByType自动装配

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

    <!--
     byType:会自动在容器上下本中查找,和自己对象属性类型相同的bean
     -->
    <bean id="people" class="com.gt.pojo.People" autowire="byType">
        <property name="name" value="山姆你好呀"/>
    </bean>

 (全部)配置文件bean.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.gt.pojo.Cat"/>
    <bean id="dog123" class="com.gt.pojo.Dog"/>

    <!--
      byName:会自动在容器中上下文中查找,和自己对象set方法后面的值对应的beanid!
       <property name="dog" ref="dog"/>
        <property name="cat" ref="cat"/>
        == autowire="byName"

        byType:会自动在容器中查找,和自己对象属性类型相同的bean!
       autowire="byType"
      -->
    <bean id="people" class="com.gt.pojo.People" autowire="byType">
        <property name="name" value="山姆你好呀"/>

    </bean>

</beans>

byName:

  • 会自动在容器中上下文中查找,和自己对象set方法后面的值对应的beanid!
  • <property name="dog" ref="dog"/>  <property name="cat" ref="cat"/> = =  autowire="byName"

byType:

  • 会自动在容器中查找,和自己对象属性类型相同的bean!
  • autowire="byType"

小结:

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

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

  • 使用byType的时候,id可以省略(可以了解并尝试)


5、使用注解实现自动装配

  • jdk1.5支持的注解,spring2.5就支持注解了!
  • The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.

要使用注解须知:

  1. 导入约束,context约束
  2. 配置注解的配置(开启注解!)<context:annotation-config/>

(1)只有context 

<?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>

(2)context➕aop

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/context/spring-aop.xsd">

    <!--  开启注解的支持-->
    <context:annotation-config/>

    <bean id="cat" class="com.gt.pojo.Cat"/>
    <bean id="dog" class="com.gt.pojo.Dog"/>
    <bean id="people" class="com.gt.pojo.People"/>


</beans>

@Autowired

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

扩展:  

  •         如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空 @Autowired(required = false)
  •         @Nullable 字段标记了这个注解,说明这个字段可以为NULL。深层代码👇
public @interface Autowired {
    boolean required() default true;
}

  • 如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,
  • 我们可以使用@Qualifier(value = "***")去配置@Autowired的使用,指定一个唯一的bean对象注入!(例子:dog123)
    <bean id="cat1" class="com.gt.pojo.Cat"/>
    <bean id="cat2" class="com.gt.pojo.Cat"/>
    <bean id="dog123" class="com.gt.pojo.Dog"/>
    <bean id="dog2" class="com.gt.pojo.Dog"/>
    <bean id="people" class="com.gt.pojo.People"/>
public class People {
    private String name;
    /**
     * 如果显式定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
     * @Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
     * @Qualifier不能单独使用
     */
    @Autowired(required = false)
    @Qualifier(value = "dog123")
    private Dog dog;
    @Autowired
    @Qualifier(value = "cat1")
    private Cat cat;
}

 

 @Resource注解

public class People {
    private String name;
    @Resource(name = "dog1")
    private Dog dog;
    @Resource(name = "cat")
    private Cat cat;
}

         @Resource要么名字一样,要么名字不一样,属性是唯一的👆都可以,如果两个都没有,才报错!

小结:

@Resource和@Autowired的区别

  • 都是用来自动装配的,都可以放到属性字段上

  • @Autowired通过byType的方式实现(ps:只根据byType匹配,不会匹配byName),而且必须要求这个对象存在!【常用】

  • @Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现,如果两个都找不到的情况就报错【常用】

  • 执行顺序不同:@Autowired通过byType的方式实现,@Resource默认通过byName的方式实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值