在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的更多相关文章

  1. Spring中的自动装配

    src\dayday\Person.java package dayday;/** * Created by I am master on 2016/11/28. */public class Per ...

  2. spring第一课,beans配置(中)——自动装配

    •Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是在 <bean> 的 autowire 属性里指定自动装配的模式 •byType(根据类型自动装配): 若 IOC 容器 ...

  3. Spring 自动装配 byType

    自动装配 byType,这种模式由属性类型指定自动装配. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType.然后,如果它的 typ ...

  4. Spring 学习——Spring注解——Autowiring(自动装配)

    装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...

  5. Spring(六)之自动装配

    一.自动装配模型 下面是自动连接模式,可以用来指示Spring容器使用自动连接进行依赖注入.您可以使用元素的autowire属性为bean定义指定autowire模式. 可以使用 byType 或者  ...

  6. Spring 由构造函数自动装配

    Spring 由构造函数自动装配,这种模式与 byType 非常相似,但它应用于构造器参数. Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 ...

  7. 6、Spring教程之自动装配

    自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean有三种装配机制,分别是: 在xml中显式配置 ...

  8. Spring autowire自动装配 ByType和ByName

    不使用自动装配前使用的是类的引用: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...

  9. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

随机推荐

  1. fabric动态获取远程目录列表

    #!/usr/bin/pythonfrom fabric.api import *env.user='root'env.hosts=['172.10.224.183','172.10.224.132' ...

  2. jQ&js给label

    <strong>当前角色:</strong><label id="lblRoleName" style="margin-bottom: 0p ...

  3. 简单Trace类实现

    <C++沉思录>27章内容修改后所得: /************************************************************************/ ...

  4. NSNumber 与NSValue

    NSNumber与NSValue关系与作用 .由于集合里只能存放对象,不可以存放基本数据类型,所以我们有时候需要讲一些对象比如基本数据类型,结构体等存到NSDictionary NSArray中,我们 ...

  5. Android获取所有应用的资源id和对应的uri

    背景 在某些应用中,为了实现应用apk资源放入重复利用,或者使用反射得到本应用的资源,需要使用反射方式获得,但Resources类中也自带了这种获取方式,并且功能更加强大 你可以获取string,co ...

  6. LeetCode第[44]题(Java):Wildcard Matching

    题目:通配符匹配 难度:hard 题目内容: Given an input string (s) and a pattern (p), implement wildcard pattern match ...

  7. js的callee和caller方法

    转载:http://www.css88.com/archives/1706 http://www.jb51.net/article/25561.htm 这里我们可以知道: caller的使用方法: f ...

  8. Codeforces Round #220 (Div. 2)

    链接 毒瘤场..... A题:,真码农题,直接干爆,枚举,注意越界问题,wa37的看这组数据1 10 1 5 2 2,应该是no //#pragma comment(linker, "/st ...

  9. 51nod 1279 单调栈

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1279 1279 扔盘子 题目来源: Codility 基准时间限制:1 ...

  10. ES6相关文章

    1.https://www.cnblogs.com/xiaotanke/p/7448383.html (export ,export default 和 import 区别 以及用法)