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>
  • 相关阅读:
    request.getRealPath的替代方法
    springmvc文件上传示例
    查询表部分列
    表名作为变量的应用
    行转列
    老男孩python学习第三天作业
    老男孩python学习第四天作业
    老男孩python学习第二天思维导图
    老男孩python学习第三天思维导图
    老男孩python学习第五天思维导图
  • 原文地址:https://www.cnblogs.com/luo-mao/p/7309573.html
Copyright © 2011-2022 走看看