码迷,mamicode.com
首页 > 编程语言 > 详细

Spring中类型自动装配--byType

时间:2017-08-09 00:07:47      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:兼容   pre   自动装配   classpath   return   this   name   int   import   

在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>

 

Spring中类型自动装配--byType

标签:兼容   pre   自动装配   classpath   return   this   name   int   import   

原文地址:http://www.cnblogs.com/luo-mao/p/7309573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!