带有@Autowired批注的Spring自动装配Bean

在最后一个XML自动装配示例中,它将自动装配当前Spring容器中任何bean的匹配属性。 在大多数情况下,您可能仅需要在特定的bean中自动装配属性。

在Spring中,可以使用@Autowired批注自动在setter方法,构造函数或字段上连接bean。 而且,它可以自动连接特定bean中的属性。

注意
@Autowired批注通过匹配数据类型自动连接bean。

请参见以下完整示例,以演示@Autowired的用法。

1.豆类

一个客户bean,并在bean配置文件中声明。 稍后,您将使用“ @Autowired ”来自动连接一个人bean。

package com.mkyong.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.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address 123" />
		<property name="age" value="28" />
	</bean>
	
</beans>

2.注册AutowiredAnnotationBeanPostProcessor

要启用@Autowired ,您必须注册' AutowiredAnnotationBeanPostProcessor ',并且您可以通过两种方式进行操作:

1.包含<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>

完整的例子

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

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<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.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address ABC" />
		<property name="age" value="29" />
	</bean>
	
</beans>

3. @Autowired示例

现在,您可以通过@Autowired自动装配bean,并将其应用于setter方法,构造函数或字段。

1. @Autowired设置器方法
package com.mkyong.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.mkyong.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.mkyong.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”自动连线到客户的person属性。

运行

package com.mkyong.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[] {"SpringBeans.xml"});
    	
    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
    	
    }
}

输出量

Customer [action=buy, type=1, 
person=Person [address=address 123, age=28, name=mkyong]]

依赖检查

默认情况下,@ Autowired将执行依赖项检查,以确保已正确连接属性。 当Spring找不到要连接的匹配bean时,它将引发异常。 要解决此问题,可以通过将@Autowired的“ required ”属性设置为false来禁用此检查功能。

package com.mkyong.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应该在字段上自动装配。 例如,具有两个相似人员bean的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.mkyong.common.Customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="PersonBean1" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
	
	<bean id="PersonBean2" class="com.mkyong.common.Person">
		<property name="name" value="mkyong2" />
		<property name="address" value="address 2" />
		<property name="age" value="28" />
	</bean>
	
</beans>

Spring会知道应该连接哪个豆吗?

要解决此问题,您可以使用@Qualifier自动连接特定的bean,例如,

package com.mkyong.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
}

这意味着,bean“ PersonBean1”将自动连接到客户的person属性中。 阅读完整的示例– Spring Autowiring @Qualifier示例

结论

@Autowired注释具有高度的灵活性和强大的功能,并且绝对优于bean配置文件中的“ autowire ”属性。

下载源代码

下载它– Spring-Autowired-Annotation-Example.zip (6 KB)

翻译自: https://mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值