Spring 专题
专题目录
您的位置:java > Spring专题 > Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配
作者:--    发布时间:2019-11-20
在上一篇 spring在xml自动装配 示例中,它会匹配当前spring容器任何bean的属性自动装配。在大多数情况下,你可能只需要在特定的 bean 自动装配属性。
在spring中,可以使用 @autowired 注解通过setter方法,构造函数或字段自动装配bean。此外,它可以在一个特定的bean属性自动装配。

注 @autowired注解是通过匹配数据类型自动装配bean。

请参见下面的完整的例子来演示如何使用@autowired。

1. beans

一个 customer bean 在bean配置文件中声明。稍后,您将使用 “@autowired” 来自动装配一个person bean。
package com.h3.common;

public class customer 
{
	//you want autowired this field.
	private person person;
	
	private int type;
	private string action;
	
	//getter and setter method
	
}
<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 id="customerbean" class="com.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<property name="address" value="address 123" />
		<property name="age" value="28" />
	</bean>
	
</beans>

2. 注册autowiredannotationbeanpostprocessor

要启用@autowired,必须注册“autowiredannotationbeanpostprocessor',你可以用两种方式做到这一点:

1. include <context:annotation-config />

添加 spring 上下文和<context:annotation-config />在bean配置文件中。
<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>

下面是完整的实例

<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.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<property name="address" value="address abc" />
		<property name="age" value="29" />
	</bean>
	
</beans>

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"/>
	
	<bean id="customerbean" class="com.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<property name="address" value="address abc" />
		<property name="age" value="29" />
	</bean>
	
</beans>

3. @autowired示例

现在,你可以通过 @autowired 自动装配 bean,它可以在 setter 方法,构造函数或字段中使用。
1. @autowired setter 方法
package com.h3.common;

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

public class customer 
{
	private person person;
	private int type;
	private string action;
	//getter and setter methods
	
	@autowired
	public void setperson(person person) {
		this.person = person;
	}
}
2. @autowired 构造方法
package com.h3.common;

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

public class customer 
{
	private person person;
	private int type;
	private string action;
	//getter and setter methods
	
	@autowired
	public customer(person person) {
		this.person = person;
	}
}
3. @autowired 字段
package com.h3.common;

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

public class customer 
{
	@autowired
	private person person;
	private int type;
	private string action;
	//getter and setter methods
}
上面的例子会自动装配“personbean”到customer的person属性。

执行它

package com.h3.common;

import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;

public class app 
{
    public static void main( string[] args )
    {
    	applicationcontext context = 
    	  new classpathxmlapplicationcontext(new string[] {"applicationcontext.xml"});
    	
    	customer cust = (customer)context.getbean("customerbean");
    	system.out.println(cust);
    	
    }
}

输出

customer [person=person [name=h3a], type=1, action=buy]

依赖检查

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

package com.h3.common;

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

public class customer 
{
	@autowired(required=false)
	private person person;
	private int type;
	private string action;
	//getter and setter methods
}
在上面的例子中,如果spring不能找到一个匹配的bean,person属性将不设定。

@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.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean1" class="com.h3.common.person">
		<property name="name" value="h3-1" />
		<property name="address" value="address-1" />
		<property name="age" value="29" />
	</bean>
	
	<bean id="personbean2" class="com.h3.common.person">
		<property name="name" value="h3-2" />
		<property name="address" value="address-2" />
		<property name="age" value="28" />
	</bean>
	
</beans>
spring知道哪个 bean 应当装配?
为了解决这个问题,可以使用 @qualifier 自动装配一个特定的 bean,例如,
package com.h3.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
} 

这意味着,“personbean1” bean被自动装配到customer的person属性。阅读下面完整的例子 – spring自动装配@qualifier实例

总结

这@autowired注解非常灵活,功能强大,绝对比bean配置文件的“autowire”属性要更好。


下载代码 –  http://pan.baidu.com/s/1o7onbto

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册