• 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;
  • 相关阅读:
    流复制-pg_basebackup (有自定义表空间)
    流复制-pg_basebackup (没有自定义表空间)
    PG 更新统计信息
    PG修改参数方法
    Postgres的索引01
    Postgres基础操作
    PostgreSQL安装
    SQL拦截器
    没对象的快自己写一个吧!带你了解一下python对象!
    喜欢看电影来哦!教你如果使用Python网络爬虫爬取豆瓣高分电影!
  • 原文地址:https://www.cnblogs.com/diaoniwa/p/6476444.html
Copyright © 2020-2023  润新知