spring使用@Autowired装载

1.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--1.使用<context:annotation-config/>
        2.使用<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>-->
    <context:annotation-config/>
    <!--<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>-->
    <bean id="CustomerBean" class="demo3.Po.Customer">
        <property name="type" value="1"/>
        <property name="action" value="buy"/>
    </bean>
    <bean id="PersonBean" class="demo3.Po.Person">
        <property name="name" value="xiaoqiang"/>
        <property name="address" value="Hongkong"/>
        <property name="age" value="18"/>
    </bean>
</beans>

2.假如出现多个bean,进行依赖检查,使用@Qualifier

xml情况:

    <bean id="CustomerBean" class="demo3.Po.Customer">
        <property name="type" value="1"/>
        <property name="action" value="buy"/>
    </bean>
    <bean id="PersonBean1" class="demo3.Po.Person">
        <property name="name" value="xiaoqiang"/>
        <property name="address" value="Hongkong"/>
        <property name="age" value="18"/>
    </bean>
    <bean id="PersonBean2" class="demo3.Po.Person">
        <property name="name" value="xiaoming" />
        <property name="address" value="Malaisia" />
        <property name="age" value="28" />
    </bean>

Pojo:

    @Autowired
    @Qualifier("PersonBean2") //选择要装配的bean
    private Person person;

    private int type;
    private String action;

 

posted @ 2017-02-27 21:37  写代码隔扣邓肯  阅读(277)  评论(0编辑  收藏  举报