python autowire库讲解_Spring使用@Autowired注解自动装配

在Spring中,可以使用 @Autowired 注解通过setter方法,构造函数或字段自动装配Bean。此外,它可以在一个特定的bean属性自动装配。

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

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

1. Beans

一个 Customer bean 在bean配置文件中声明。稍后,您将使用 “@Autowired” 来自动装配一个Person bean。

package com.yiibai.common;

public class Customer

{

//you want autowired this field.

private Person person;

private int type;

private String action;

//getter and setter method

}

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

2. 注册AutowiredAnnotationBeanPostProcessor

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

1. Include

添加 Spring 上下文和在bean配置文件中。

//...

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

//...

//...

下面是完整的实例

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

2.  包含 AutowiredAnnotationBeanPostProcessor

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

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

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

3. @Autowired示例

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

1. @Autowired setter 方法

package com.yiibai.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.yiibai.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.yiibai.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.yiibai.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=YiibaiA], type=1, action=buy]

依赖检查

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

package com.yiibai.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 配置文件。

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

Spring知道哪个 bean 应当装配?

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

package com.yiibai.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”属性要更好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值