Spring5框架四:Bean的自动装配

自动装配是Spring满足bean依赖的一种方式 ,Spring会在上下文中自动寻找,并自动给bean装配属性

三种装配的方式

在xml中显示的配置

之前写的都是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="com.www.pojo.Cat"></bean>
    <bean id="dog" class="com.www.pojo.Dog"></bean>

    <bean id="people" class="com.www.pojo.People">
        <property name="name" value="嘵奇"></property>
        <property name="dog" ref="dog"></property>
        <property name="cat" ref="cat"></property>
    </bean>
</beans>
在java中显示的配置
隐式的自动装配bean(重要)
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="com.www.pojo.Cat"></bean>
    <bean id="dog" class="com.www.pojo.Dog"></bean>
<!--byName:会自动在上下文中查找和自己对象set方法后面的值对应的beanid-->
    <bean id="people" class="com.www.pojo.People" autowire="byName">
        <property name="name" value="嘵奇"></property>
    </bean>


</beans>
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">

<!--byType:会自动在上下文中查找和自己对象属性类型相同的bean-->
    <bean class="com.www.pojo.Cat"></bean>
    <bean class="com.www.pojo.Dog"></bean>
    <bean id="people" class="com.www.pojo.People" autowire="byType">
        <property name="name" value="嘵奇"></property>
    </bean>

</beans>

注意:
使用ByName的时候需要保证bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
使用ByType的时候需要保证所有bean的class唯一,并且这个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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">


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

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

在实体类上使用

package com.www.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 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 + '\'' +
                '}';
    }
}

注意:直接在属性上使用,也可以在set方法上使用
使用Autowired可以不用编写set方法了,前提是属性在IOC容器中存在且符合名字byname

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值