[学习记录]Spring自动装配Bean

Spring自动装配Bean

在spring框架,可以通过auto-wiring功能自动装配Bean,在<bean>中定义autowire属性。

在spring中有物种自动装配模式

  • no -缺省情况下,手动使用ref设定。

  • byName -根据属性名称自动装配。

  • byType -根据数据类型自动装配。如果一个bean的数据类型使用其他bean属性的数据类型,则会自动装配。

  • constructor -在构造函数的byType方式

  • autodetect -如果找到默认的构造函数,使用“自动装配用构造“;否则,使用“按类型自动装配”。

    在开始之前我已经创建了一个spring项目,详情可以看我前面的文章CSDN.

现在重新创建两个bean,分别是Speciality、Person

package com.spring.entity;

public class Speciality {

    private String skill;

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }
}
package com.spring.entity;

public class Person {
    private Speciality speciality;

    public Speciality getSpeciality() {
        return speciality;
    }

    public void setSpeciality(Speciality speciality) {
        this.speciality = speciality;
    }
}

1.no

默认模式,通过‘ref’来连接bean

    <bean id="speciality" class="com.spring.entity.Speciality">
        <property name="skill" value="打篮球" />
    </bean>

    <bean id="person" class="com.spring.entity.Person">
        <property name="speciality" ref="speciality" />
    </bean>

输出结果

Speciality{skill='打篮球'}

如果使用按名装配,只需要修改第二个bean:

2.byName

如果使用按名装配,只需要修改第二个bean:

    <bean id="person" class="com.spring.entity.Person" autowire="byName" />

输出结果

Speciality{skill='打篮球'}

这里要注意,如果使用按名装配,bean的id名必须与属性名相同,否则会装配失败。

3.byType

通过按类型装配,修改第二个bean的autowire:

    <bean id="person" class="com.spring.entity.Person" autowire="byType" />

输出结果

Speciality{skill='打篮球'}

这里也要注意,当按类型装配时,如果有两个bean的类型都相同时,则会输出错误信息。

下面这种配置就是错误的配置

    <bean id="person" class="com.spring.entity.Person" autowire="byType" />

    <bean id="speciality" class="com.spring.entity.Speciality">
        <property name="skill" value="打篮球" />
    </bean>
    <bean id="speciality2" class="com.spring.entity.Speciality">
        <property name="skill" value="踢足球" />
    </bean>

一般使用idea时会直接提示错误信息。

4.通过构造方法自动装配

现在在Person类中添加构造方法。

   public Person(Speciality speciality) {
        this.speciality = speciality;
   }

在spring-conf.xml中修改:

    <bean id="person" class="com.spring.entity.Person" >
        <constructor-arg>
            <ref bean="speciality" />
        </constructor-arg>
    </bean>

输出结果

Speciality{skill='打篮球'}

也可以不设置构造器属性。Spring会找到兼容的数据类型,并自动装配它。

    <bean id="person" class="com.spring.entity.Person" autowire="constructor" />

5.使用@Autowired注解装配

@Autowired注解装配是通过匹配数据类型进行装配

首先修改在Person类:

package com.spring.entity;

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

public class Person {
    @Autowired
    private Speciality speciality;

    public void personTest(){
        speciality.getSkill();
    }
}

在使用注解之前需要修改spring-conf.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

     <context:annotation-config/><!-- 开启注解扫描 -->
    <bean id="person" class="com.spring.entity.Person">

    </bean>
    <bean id="speciality" class="com.spring.entity.Speciality">
        <property name="skill" value="打篮球"/>
    </bean>
<beans>

注意:通常情况下spring会执行检查,确保属性已经装配正确,如果无法找到属性装配,则会抛出异常。通过 @Autowired 的“required”属性设置为false来禁用此检查功能。

package com.spring.entity;

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

public class Person {
    @Autowired(required = false)
    private Speciality speciality;

    public void personTest(){
        speciality.getSkill();
    }
}

上述代码中,如果spring找不到名字为speciality的bean,则Speciality不会进行装配

当然,如果bean的名字和属性名不同。可以使用 @Qualifier 自动装配一个特定的 bean。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值