Spring Bean的自动装配

        Bean的装配可以理解为依赖关系注入,Bean的装配方式,即Bean的依赖注入方式,Spring容器支持多种形式的Bean的装配方式:

eg:

实体类 people、cat、dog

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



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


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

在XML中显式配置(基于XML的装配) :
             设值注入(Set方式注入)(Setter Injection)和构造器注入(Constructor Injection)在Spring实例化Bean的过程中,Spring首先会调用Bean的默认构造方法来实例化Bean对象,然后通过反射的方式调用setter()方法来注入属性值。因此,设值注入要求一个Bean必须满足要求:Bean类必须提供一个无参构造方法。
        Bean类必须为需要注入的属性提供对应的setter()方法
设值注入:在配置文件中,需要使用 <property>元素为每个属性注入值
构造注入:在配置文件中,需要使用<constructor-arg>元素来定义构造方法的参数,也可以使用其value属性来设置该参数的值
Spring DI依赖注入_张志明(努力奋斗版)的博客-CSDN博客1.构造器注入如果只有一个有参数的构造方法并且参数类型与注入的bean的类型匹配,那就会注入到该构造方法中。要求类里要有有参构造。每个构造参数就是一个依赖项 详见下文中:使用有参数构造创建对象Spring IOC创建对象的方式_张志明(努力奋斗版)的博客-CSDN博客1.默认使用无参构造器创建对象 tips:要求实体类中不显示写出构造函数 或者 写出缺省的构造函数.xml <?xml version="1.0" encoding="UTF-8"?><beans xml...https://blog.csdn.net/TIANJIAWEN/article/details/124800270?spm=1001.2014.3001.5501

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

    <bean id="people" class="com.kero.pojo.People">
        <property name="name" value="kk"/>
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
     </bean>
</beans>

自动装配 
        将一个Bean自动地注入到其他Bean的Property中

        Spring的<bean>元素中包含一个autowired的属性,我们可以设置autowired的属性值来自动装配Bean

1.ByName自动装配
        会自动在容器上下文中查找和自己对象set方法后面的值对应的bean id

        tips:必须名字相同,但是set方法后面名字是大写,可以用小写,但是用大写不行
eg:public void setDog22(Dog dog) { this.dog = dog; }  
        <bean id="DOG22" class="com.kero.pojo.Dog"/> 这样不行
        <bean id="dog22" class="com.kero.pojo.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.kero.pojo.Cat"/>
    <bean id="dog" class="com.kero.pojo.Dog"/>

    <bean id="people" class="com.kero.pojo.People" autowire="byName">
        <property name="name" value="kk"/>
     </bean>
</beans>

2.ByType自动装配
        会自动在容器上下文中查找,和自己对象属性类型相同的bean 
        tips:必须保证这个类型全局唯一,如果有两个相同的
        可以省略id

eg:
        <bean class="com.kero.pojo.Cat"/>
        <bean class="com.kero.pojo.Dog"/>

3.使用注解实现自动装配
        注解使用须知:
                ①导入约束 (导入context约束和两个约束的支持)

xmlns:context="http://www.springframework.org/schema/context"

                约束的支持要写在xsi:schemaLocation=""引号中

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

               ②配置注解的支持   

 <!--开启注解的支持-->
<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
        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/>

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

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

@Autowired 直接在属性上使用即可 (此时可以省略set方法) 

package com.kero.pojo;

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

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

    public Cat getCat() {
        return cat;
    }


    public Dog getDog() {
        return dog;
    }

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

    public String getName() {
        return name;
    }


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

也可以在set方法上使用

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

public class People {

    private Cat cat;

    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }
    @Autowired
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    @Autowired
    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Dog getDog() {
        return dog;
    }

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

    public String getName() {
        return name;
    }


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

//如果显示定义了required = false 说明这个对象可以为null  不显示定义的话 不能为null
@Autowired(required = false) 


@Autowired 默认按照Bean的类型进行装配  
@Qualifier:与@Autowired注解配合使用,会将默认的按Bean类型装配修改为Bean的实例名称装配,Bean的实例名称由 @Qualifier注解的参数指定

eg:此时不报错 因为默认按照Bean的类型进行装配

<bean id="cat1" class="com.kero.pojo.Cat"/>
<bean id="dog1" class="com.kero.pojo.Dog"/>

 此时报错 匹配不到相同的 bean         @Autowired是按类型自动装配的,不支持id匹配。

<bean id="cat2" class="com.kero.pojo.Cat"/>
<bean id="dog2" class="com.kero.pojo.Dog"/>
<bean id="cat1" class="com.kero.pojo.Cat"/>
<bean id="dog1" class="com.kero.pojo.Dog"/>


但是如果在people.java做如下更改 就不报错了  

    @Autowired
    @Qualifier(value ="cat1")
    private Cat cat;
    @Autowired
    @Qualifier(value ="dog1")
    private Dog dog;

        若@Autowired自动装配的环境比较复杂 自动装配无法通过一个注解@Autowired完成的时候 我们可以使用@Qualifier(value="XXX")去配置@Autowired的使用 指定唯一的bean对象注入
 补充知识:
        @Nullable 字段标记了这个注解 说明这个字段可以为null

@Resource
        @Resource:其作用与@Autowired一样。其区别在于@Autowired默认Bean的类型装配,而@Resource默认按照Bean的实例名称进行装配

    @Resource(name = "cat2")
    private Cat cat;
    @Resource(name = "dog2")
    private Dog dog;
<bean id="cat2" class="com.kero.pojo.Cat"/>
<bean id="dog2" class="com.kero.pojo.Dog"/>
<bean id="cat1" class="com.kero.pojo.Cat"/>
<bean id="dog1" class="com.kero.pojo.Dog"/>

@Resource VS @Autowired
       
·都是用来自动装配的都可以放在属性字段上
        ·@Autowired 通过byname的方式实现 而且必须要求这个对象存在
        ·@Resource 默认通过byName的方式实现 若找不到名字则通过byType实现 
   若两个都找不到 则报错!
        ·执行顺序不同@Autowired通过byname的方式实现@Resource 默认通过byName的方式实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值