Spring自动封装Bean

本文详细介绍了Spring框架中Bean的自动装配机制,包括五种自动装配模式:'no'、'byName'、'byType'、'constructor'和'autodetect'。同时,深入探讨了如何使用@Autowired注解进行自动装配,以及其依赖检查和@Qualifier注解的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring 自动封装Bean


在Spring框架中,可以用auto—wiring功能自动装配Bean。要启用它,只需要在xml配置文件中标签定义“autowire”属性:

<bean id="customer" class="com.myprj.common.Customer" autowire="byName" />

在Spring中,支持5中自动装配模式:

示例:Customer和Person对象的自动装配示例

package com.myprj.common;

public class Customer 
{
	private Person person;
	
	public Customer(Person person) {
		this.person = person;
	}
	
	public void setPerson(Person person) {
		this.person = person;
	}
	//...
}
package com.myprj.common;

public class Person 
{
	//...
}

一、Auto-Wiring ‘no’


这是默认模式,你需要通过‘ref’属性来连接bean:

<bean id="customer" class="com.myprj.common.Customer">
                  <property name="person" ref="person" />
	</bean>

	<bean id="person" class="com.myprj.common.Person" />

二、Auto-Wiring ‘byName’


按属性名称自动装配。在这种情况下,由于对“person” bean的名称是相同于“customer” bean 的属性(“person”)名称,所以,Spring会自动通过setter方法将其装配 – “setPerson(Person person)“:

<bean id="customer" class="com.myprj.common.Customer" autowire="byName" />	
<bean id="person" class="com.myprj.common.Person" />

三、Auto-Wiring ‘byType’


通过按属性的数据类型自动装配Bean。在这种情况下,由于“Person” bean中的数据类型是与“customer” bean的属性(Person对象)的数据类型一样的,所以,Spring会自动通过setter方法将其自动装配。– “setPerson(Person person)“:

<bean id="customer" class="com.myprj.common.Customer" autowire="byType" />
<bean id="person" class="com.myprj.common.Person" />

四、Auto-Wiring ‘constructor’


通过构造函数参数的数据类型按属性自动装配Bean。在这种情况下,由于“person” bean的数据类型与“customer” bean的属性(Person对象)的构造函数参数的数据类型是一样的,所以,Spring通过构造方法自动装配 – “public Customer(Person person)“:

<bean id="customer" class="com.myprj.common.Customer" autowire="constructor" />
	
	<bean id="person" class="com.myprj.common.Person" />

五、Auto-Wiring ‘autodetect’


“通过构造函数自动装配”是指选,如果默认构造函数(参数与任何数据类型)不匹配时,以其他方式使用“按类型自动装配”:

<bean id="customer" class="com.myprj.common.Customer" autowire="autodetect" />
	
	<bean id="person" class="com.myprj.common.Person" />

六、Spring使用@Autowired注解自动装配


在Spring中,可以使用@Autowired注解通过setter方法、构造函数或字段自动装配Bean,注意:@Autowired注解是通过匹配数据类型自动装配Bean的。

1. 注册AutowiredAnnotationBeanPostProcessor

要启用@Autowired,必须注册“AutowiredAnnotationBeanPostProcessor",,你可以用以下两种方式做到这一点:

1.1 Include<context:annotation-config />

在Bean配置文件中添加Spring上下文和<context:annotation-config />:

<beans 
	//...
	xmlns:context="http://www.springframework.org/schema/context"
	//...
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	//...

	<context:annotation-config />
	//...
</beans>
1.2 包含 AutowiredAnnotationBeanPostProcessor

直接在bean配置文件包含“AutowiredAnnotationBeanPostProcessor”:

<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-2.5.xsd">

<bean 
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	
//...
	
</beans>

2. @Autowired使用

现在,你可以通过 @Autowired 自动装配 bean,它可以在 setter 方法,构造函数或字段中使用。

2.1 依赖检查

默认情况下,@Autowired将执行相关检查,以确保属性已经装配正常。当Spring无法找到匹配的Bean装配,它会抛出异常。要解决这个问题,可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能,如下,如果Spring不能找到一个匹配的Bean,person属性将不设定:

	@Autowired(required=false)
	private Person person;
2.2 @Qualifier

@Qualifier注解我们用来控制bean应在字段上自动装配。例如,具有两个类似的 person bean 配置文件:

<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-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:annotation-config />

	<bean id="CustomerBean" class="com.myprj.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean1" class="com.myprj.common.Person">
		<property name="name" value="myprj-1" />
		<property name="address" value="address-1" />
		<property name="age" value="29" />
	</bean>
	
	<bean id="PersonBean2" class="com.myprj.common.Person">
		<property name="name" value="myprj-2" />
		<property name="address" value="address-2" />
		<property name="age" value="28" />
	</bean>
	
</beans>

Spring知道哪个 bean 应当装配?

为了解决这个问题,可以使用 @Qualifier 自动装配一个特定的 bean,例如:

package com.myprj.common;

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

public class Customer 
{
	@Autowired
	@Qualifier("PersonBean1")
	private Person person;
	private int type;
	private String action;
	//getter and setter methods
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值