zoukankan      html  css  js  c++  java
  • Spring中类型自动装配--byType

    在Spring中,“类型自动装配”的意思是如果一个bean的数据类型与其它bean属性的数据类型相同,将自动兼容装配它。
    例如,一个“persion” bean 公开以“ability”类数据类型作为属性,Spring会找到ability类相同的数据类型,并自动装配它的Bean。如果没有匹配找到,它什么也不做。
     
    package auto_w;
    
    /**
     * Created by luozhitao on 2017/8/8.
     */
    public class ablity {
    
        public void setWrite_able1(String write_able1) {
            this.write_able1 = write_able1;
        }
    
        public String getWrite_able1() {
            return write_able1;
        }
    
        private String write_able1;
    }
    package auto_w;
    
    /**
     * Created by luozhitao on 2017/8/8.
     */
    public class Person {
    
        public void setLity(ablity lity) {
            this.lity = lity;
        }
    
        public ablity getLity() {
            return lity;
        }
    
        private ablity lity;
    }
    package auto_w;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by luozhitao on 2017/8/8.
     */
    public class p_app {
    
        public static void main(String [] args){
    
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
    
        Person person=(Person)context.getBean("person");
        System.out.println(person.getLity().getWrite_able1());
    
    }
    }
     <bean id="person" class="auto_w.Person" autowire="byType"></bean>
        <bean id="ablity" class="auto_w.ablity">
            <property name="write_able1" value="write"/>
        </bean>
  • 相关阅读:
    HDU 2842 (递推+矩阵快速幂)
    HDU 2838 (DP+树状数组维护带权排序)
    HDU 2836 (离散化DP+区间优化)
    HDU 2831 (贪心)
    HDU 2818 (矢量并查集)
    HDU 2822 (BFS+优先队列)
    HDU 3090 (贪心)
    HDU 3089 (快速约瑟夫环)
    XCOJ 1103 (LCA+树链最大子段和)
    HDU 3078 (LCA+树链第K大)
  • 原文地址:https://www.cnblogs.com/luo-mao/p/7309573.html
Copyright © 2011-2022 走看看