zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    如何面试程序员?
    开始做项目
    ===
    依赖注入获得一个对象却想返回不同的值(Error)
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]ResultSet can not reread row data for column 4.
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed.
    .net 4.5新特性
    有限状态机简单示例
    JavaScript入门经典(第四版)文摘
    小强升职记读后感
  • 原文地址:https://www.cnblogs.com/diaoniwa/p/6476444.html
Copyright © 2011-2022 走看看