Spring使用@Autowired注解自动装配

来源:互联网 发布:淘宝充值方式 编辑:程序博客网 时间:2024/05/21 19:48

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

下面通过一个例子来实现通过@Autowired来进行自动装配

1.首先配置俩个bean,建立俩个bean之间的关联关系。建立配置文件

package com.hebeu.model;public class Person {private int age;private String name;private String addrss;public Person(){}public Person(int age, String name, String addrss) {super();this.age = age;this.name = name;this.addrss = addrss;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddrss() {return addrss;}public void setAddrss(String addrss) {this.addrss = addrss;}}
package com.hebeu.model;public class Customer {private int type;private String action;private Person person;public Customer(){}public Customer(int type, String action, Person person) {super();this.type = type;this.action = action;this.person = person;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getAction() {return action;}public void setAction(String action) {this.action = action;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}}
建立这俩个bean,他俩之间有关联关系。

2. 注册AutowiredAnnotationBeanPostProcessor:它的作用是当Spring容器启动时,AutowiredAnnotationBeanPostProcessor将自动扫描Spring容器中所有的bean,当发现bean中拥有@Autowired注释时就找到和其匹配的bean,并注入到对应的地方去。

注册AutowiredAnnotationBeanPostProcessor有俩种方法可以做到。

   1.添加Spring上下文和<context:annotation-config />到配置文件中。

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config /><!-- 配置顾客这个bean --><bean name="customer" class="com.hebeu.model.Customer"><property name="type" value="1" /><property name="action" value="buy" /></bean><bean name="person" class="com.hebeu.model.Person"><property name="age" value="12"/><property name="name" value="张三"/><property name="address" value="河北省张家口市"/></bean></beans> 
   2.直接在配置文件中包含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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/><bean name="customer" class="com.hebeu.model.Customer"><property name="type" value="1" /><property name="action" value="buy" /></bean><bean name="person" class="com.hebeu.model.Person"><property name="age" value="12"/><property name="name" value="张三"/><property name="address" value="河北省张家口市"/></bean></beans> 

3.在bean中使用@Autowired注解,他可以在setter方法,构造函数以及字段中使用

      1.在setter()方法上添加注解

package com.hebeu.model;import org.springframework.beans.factory.annotation.Autowired;public class Customer {private int type;private String action;private Person person;public Customer(){}public Customer(int type, String action, Person person) {super();this.type = type;this.action = action;this.person = person;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getAction() {return action;}public void setAction(String action) {this.action = action;}public Person getPerson() {return person;}@Autowiredpublic void setPerson(Person person) {this.person = person;}}

        2.在构造方法中使用【要求构造方法中只有一个被bean变量】

package com.hebeu.model;import org.springframework.beans.factory.annotation.Autowired;public class Customer {private int type;private String action;private Person person;public Customer(){}@Autowiredpublic Customer(Person person) {this.person = person;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getAction() {return action;}public void setAction(String action) {this.action = action;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}}

        3.在字段中使用

package com.hebeu.model;import org.springframework.beans.factory.annotation.Autowired;public class Customer {private int type;private String action;@Autowiredprivate Person person;public Customer(){}public Customer(Person person) {this.person = person;}public int getType() {return type;}public void setType(int type) {this.type = type;}public String getAction() {return action;}public void setAction(String action) {this.action = action;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}}

在实际中,在字段上进行注解最为常用.通常在JavaEE项目中,对bean进行注入的时候,有时候我们会使用@Resource注解,这个也可以实现自动装配bean的功能。下面简单的区别下这俩个注解:

@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:

@Autowired() @Qualifier("baseDao"

private BaseDao baseDao;   


@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

@Resource(name="baseDao")   

private BaseDao baseDao;   


1 1
原创粉丝点击