您的位置:首页 > 编程语言 > Java开发

4、spring使用@Autowired注解实现自动装配

2017-02-18 13:16 946 查看
在上一篇spring的五种自动装配方式 教程中,我们了解到spring利用xml方式进行操作的五种自动装配方式,而在这篇文章中我们将学习利用@Autowired注解的方式进行自动装配

@Autowired注解自动装配的三种方式

setter方式

构造函数方式

通过字段自动装配方式

例子

第一步:创建bean

Customer.java

package com.main.autowrite.autowired.annotation;

public class Customer {
//即将自动装配的属性
private Person person;
private int type;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}


Person.java

package com.main.autowrite.autowired.annotation;

public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}


第二步:配置beans配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"> <bean id="customer"
class="com.main.autowrite.autowired.annotation.Customer" >
<property name="type" value="user"></property>
</bean>
<bean id="person" class="com.main.autowrite.autowired.annotation.Person">
<property name="name" value="jack" />
<property name="age" value="18"/>
</bean>
</beans>


第三步:注册AutowiredAnnotationBeanPostProcessor

主要有以下两种方式进行注册

在beans配置文件中添加spring上下文和“context:annotation-config”标签

包含 AutowiredAnnotationBeanPostProcessorbean

第一种:添加 Spring 上下文和context:annotation-config在beans配置文件中。

更改你的beans配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"> <context:annotation-config />
<bean id="customer"
class="com.main.autowrite.autowired.annotation.Customer" >
<property name="type" value="user"></property>
</bean>
<bean id="person" class="com.main.autowrite.autowired.annotation.Person">
<property name="name" value="jack" />
<property name="age" value="18"/>
</bean>
</beans>


第二种:包含AutowiredAnnotationBeanPostProcessor

更改你的beans配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"> <bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="customer"
class="com.main.autowrite.autowired.annotation.Customer" >
<property name="type" value="user"></property>
</bean>
<bean id="person" class="com.main.autowrite.autowired.annotation.Person">
<property name="name" value="jack" />
<property name="age" value="18"/>
</bean>
</beans>


第四步:使用@Autowired注解进行自动装配

当你已经完成上述步骤后,此时你就可以使用@Autowired注解进行自动装配了。

主要有以下[b]三种
方式:

通过setter方法

通过constructor构造方法

直接在字段属性声明时使用@Autowired注解进行自动装配

第一种:通过setter方法

修改你的Customer类如下:

package com.main.autowrite.autowired.annotation;

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

public class Customer {
//即将自动装配的属性
private Person person;
private int type;
public Person getPerson() {
return person;
}
@Autowired
public void setPerson(Person person) {
this.person = person;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}


第二种:通过constructor构造方法

修改你的Customer类如下

package com.main.autowrite.autowired.annotation;

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

public class Customer {
//即将自动装配的属性
private Person person;
private String type;
@Autowired
public Customer(Person person) {
this.person = person;
}

//getter and setter methods
//toString methods
}


第三种:

修改你的Customer类如下

package com.main.autowrite.autowired.annotation;

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

public class Customer {
//即将自动装配的属性
@Autowired
private Person person;
private String type;
//getter and setter methods
//toString methods
}


第五步:测试(上述第四步的三种装配方式均可使用以下方式进行测试)

@Test
public void test(){

ApplicationContext context =
new ClassPathXmlApplicationContext("com/main/autowrite/autowired/annotation/beans.xml");

Customer customer = (Customer)context.getBean("customer");

System.out.print(customer.toString());
}


测试结果:



结论:使用@Autowired注解实现自动装配非常灵活,而且功能强大。

在这里可以思考一下,如果存在多个person bean,那么customer将会自动装配哪一个??可以参考下一篇关于@Qualifier注解的使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring