如何实现Spring中Bean的自动装配

Bean的自动装配

自动装配是使用spring满足bean依赖的一种方法
spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring中bean有三种装配机制,分别是:

  1. 在xml中显式配置;
  2. 在java中显式配置;
  3. 隐式的bean发现机制和自动装配。
    这里我们主要讲第三种:自动化的装配bean。
    Spring的自动装配需要从两个角度来实现,或者说是两个操作:
    1.组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
    2.自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;

    组件扫描和自动装配组合发挥巨大威力,使的显示的配置降低到最少。
    推荐不使用自动装配xml配置 , 而使用注解 .

准备工作

创建实体类
people类

public class People {

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

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

    public People() {

    }

    public People(Dog dog, Cat cat, String name) {
        this.dog = dog;
        this.cat = cat;
        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;
    }

    public String getName() {
        return name;
    }

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

cat类

public class Cat {

    public void cry(){
        System.out.println("喵喵喵");
    }

}

dog类

public class Dog {

    public void cry(){
        System.out.println("汪汪汪");
    }
}

导入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

IOC容器-applicationContext.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="people" class="com.pojo.People">
        <property name="name" value="熊宝"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
    </bean>

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

</beans>

测试

	@Test
    public void text(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        People p1 =(People) context.getBean("people");
        p1.getCat().cry();
        p1.getDog().cry();
    }

打印结果

喵喵喵
汪汪汪

1.使用xml进行自动装配

  1. byName ,会自动在容器上下文中查找和自己对象set方法后面的值对应的beanid (通过对象的set方法中set后面的值与bean的id进行匹配) 这种方法的弊端在于id 的名字必须与set匹配
<bean id="people" class="com.pojo.People" autowire="byName">
        <property name="name" value="熊宝"/>
    </bean>

    <bean id="cat" class="com.pojo.Cat"></bean>
    <bean id="dog" class="com.pojo.Dog"></bean>
  1. byType , 会自动在容器上下文中查找和自己对象属性类型相同的bean 这种方法的弊端在于匹配的类型是在全局的上下文在查找的 , 如果有多个类型匹配会报错
<bean id="people" class="com.pojo.People" autowire="byType">
        <property name="name" value="熊宝"/>
    </bean>
	<!-- 通过类型自动装配不需要id -->
    <bean  class="com.pojo.Cat"></bean>
    <bean  class="com.pojo.Dog"></bean>

使用注解自动装配

准备工作

  1. 在spring配置文件中引入context文件头
xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
  1. 开启属性注解支持!
<context:annotation-config/>

将上面的案例改造后

<?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="people" class="com.pojo.People" autowire="byType"></bean>
    <bean id="cat" class="com.pojo.Cat"></bean>
    <bean id="dog" class="com.pojo.Dog"></bean>

</beans>

接下来就可以使用@Autowired注解进行装配啦~
使用了@Autowired后就不再需要set方法 , 前提是你这个自动装配的属性在IOC容器中存 或 符合byName (beanid 和属性名必须相等)!

  • 没有指定id且上下文中包含多个相同类型的bean ------ 会报错

修改people类~

public class People {

    @Autowired
    private Dog dog;

    @Autowired
    private Cat cat;
    private  String name;

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

    public People() {

    }

    public People(Dog dog, Cat cat, String name) {
        this.dog = dog;
        this.cat = cat;
        this.name = name;
    }

    public Dog getDog() {
        return dog;
    }

    public Cat getCat() {
        return cat;
    }

    public String getName() {
        return name;
    }

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

测试,成功输出结果!

喵喵喵
汪汪汪

注意点:
@Autowired(required=false) 说明: false,对象可以为null ,就是容器中找不到对应类型的bean也不会报错 ;true,对象必须存对象,不能为null

如果没有指定id且有多个相同类型的bean , 可以使用**@Qualifier(value = “”)**注解
在value中著名指定id即可

	@Autowired
    @Qualifier(value = "dog2")
    private Dog dog;
<bean id="dog1" class="com.pojo.Dog"></bean>
<bean id="dog2" class="com.pojo.Dog"></bean>

使用@Resource自动装配

  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;
  • 其次再进行默认的byName方式进行装配;
  • 如果以上都不成功,则按byType的方式自动装配。
  • 都不成功,则报异常。
	@Resource(name = "dog1")
    private Dog dog;

    @Resource
    private Cat cat;
	<bean id="people" class="com.pojo.People" autowire="byType"></bean>
    <bean id="cat1" class="com.pojo.Cat"></bean>
    <bean id="dog1" class="com.pojo.Dog"></bean>
    <bean id="dog2" class="com.pojo.Dog"></bean>

测试结果和上面一样 ,

关于@Autowired和@Resource的区别

转载自 点击这里


@Resource
的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

@Resource装配顺序
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Springbean的自动装配有几种方式。一种是通过ByType自动装配,即Spring会自动在容器上下文查找和自己对象属性类型相同的bean,并将其注入到属性。需要注意的是,所有的bean的class必须唯一,并且这个bean需要和自动注入的属性保持一致。可以在配置文件使用autowire="byType"来实现这种自动装配方式。\[1\] 另一种方式是使用注解实现自动装配。可以在bean的属性上使用@Autowired注解,Spring会自动在容器上下文查找和属性类型相同的bean,并将其注入到属性。需要在配置文件加入context约束,即在配置文件的根元素添加context命名空间的声明。\[2\]\[3\] 总结起来,Springbean自动装配可以通过ByType自动装配和使用注解实现自动装配两种方式来实现。 #### 引用[.reference_title] - *1* [SpringBean的自动装配](https://blog.csdn.net/weixin_44107140/article/details/119154162)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [springbean的自动装配(详细)](https://blog.csdn.net/qijing19991210/article/details/119712328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值