Spring@Autowired注解

@Autowired注解可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
注意:@Autowired默认是按照类型来注入的。
看下面的例子:例子是以对成员变量(field)为例进行的

public class Person {
    private String name;
    private String address;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;

    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public String toString() {
        return "[name:"+getName()+
                ",address:" + getAddress()+
                ",age:" +getAge()+
                "]";
    }

另外一个Customer类有一个Person类型的成员,现用@Autowired注入:

public class Customer
{
    @Autowired
    private Person person;
    private int type;
    private String action;

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "[Type:"+getType()+
                ",action:"+getAction()+
                ","+"person:"+
                person.toString();
    }
}

在配置文件中加入

<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">
    <!--@Autowired-->
    <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>

测试程序:

public class Main 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
          new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});

        Customer customer  = (Customer)context.getBean("CustomerBean");
        System.out.println(customer);
    }
}

@Autowired默认情况下,总是认为field是可以成功注入的,如果没有成功注入(例如没有匹配的类型)则会跑出异常,如果要使得在没有成功注入的情况下不抛出异常,那么可以和required属性一起使用,将required的属性设置为false:

public class Customer 
{
    **@Autowired(required=false)**
    private Person person;
    private int type;
    private String action;
    //getter and setter methods
}

@Autowired默认是按照类型来匹配的,那么当容器中有两个相同类型的bean时怎样区分要注入是哪一个呢?这时候@Qualifier注解便起作用了:

 <bean id="person1" class="com.hzsunpeng.Person">
        <property name="name" value="name1"/>
        <property name="address" value="adderss1"/>
        <property name="age" value="23"/>
    </bean>
    <bean id="person2" class="com.hzsunpeng.Person">
        <property name="name" value="name2"/>
        <property name="address" value="adderss2"/>
        <property name="age" value="22"/>
    </bean>

这时候怎样决定要注入person1呢还是person2呢?

如果想注入person1,那么可以这样做:

public class Customer
{
    @Autowired
    @Qualifier("person1")
    private Person person;
    private int type;
    private String action;
    //setter and getter
}

最后注意一个细节:如果使用了springMVC自动扫描组件(@Component或者是@Service等),在配置文件中加入了

<context:component-scan base-package="***.***.***" />

那么配置文件中的

<context:annotation-config/>

可以省略。

posted @ 2016-05-30 21:36  sunp823  阅读(206)  评论(0编辑  收藏  举报