本篇介绍一下自动装配的知识,Spring为了简化配置文件的编写。采用自动装配方式,自动的装载需要的bean。

  自动装配 有以下几种方式:

  1 byName 通过id的名字与属性的名字进行判断,要保证Bean实例中属性名字与该装配的id名字相同。

  2 byType 通过类型确定装配的bean,但是当存在多个类型符合的bean时,会报错。

  3 contructor 在构造注入时,使用该装配方式,效果如同byType。

  4 autodetect 自动装配,这个测试了,3.0.5版本不可用了,不知道是不是被移除了。

  下面简单的看下,自动装配的所需代码:

public class Instrumentalist implements Performer{
private String song;
private int age;
private Instrument instrument;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public Instrumentalist(){}
public Instrumentalist(String song,int age,Instrument instrument){
this.song = song;
this.age = age;
this.instrument = instrument;
}
public void perform() throws PerformanceException {
System.out.println("Instrumentalist age:"+age);
System.out.print("Playing "+song+":");
instrument.play();
}
}
public interface Instrument {
public void play();
}
public class Saxophone implements Instrument {
public Saxophone(){}
public void play() {
System.out.println("TOOT TOOT TOOT");
}
}
public class test {
public static void main(String[] args) throws PerformanceException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
performer.perform(); }
}

  采用byName方式的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="instrument" class="com.spring.test.setter.Saxophone"/>
<bean id="kenny" class="com.spring.test.setter.Instrumentalist" autowire="byName">
<property name="song" value="Jingle Bells" />
<property name="age" value="" />
</bean>
</beans>

  采用byType的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="test2" class="com.spring.test.setter.Saxophone"/>
<bean id="test1" class="com.spring.test.setter.Saxophone" primary="true"/> <!-- 默认是false -->
<bean id="kenny" class="com.spring.test.setter.Instrumentalist" autowire="byType">
<property name="song" value="Jingle Bells" />
<property name="age" value="" />
</bean>
</beans>

  如果有多个类型匹配的bean,则可以采用 primary 来设置主要装配的bean,默认情况下是false。

【Spring实战】—— 8 自动装配的更多相关文章

  1. Spring(六)之自动装配

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

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

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

  3. 【面试普通人VS高手系列】Spring Boot中自动装配机制的原理

    最近一个粉丝说,他面试了4个公司,有三个公司问他:"Spring Boot 中自动装配机制的原理" 他回答了,感觉没回答错误,但是怎么就没给offer呢? 对于这个问题,看看普通人 ...

  4. Spring学习笔记--自动装配Bean属性

    Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...

  5. Spring基于注解自动装配

    前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...

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

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

  7. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  8. Spring实战2:装配bean—依赖注入的本质

    主要内容 Spring的配置方法概览 自动装配bean 基于Java配置文件装配bean 控制bean的创建和销毁 任何一个成功的应用都是由多个为了实现某个业务目标而相互协作的组件构成的,这些组件必须 ...

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

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

  10. spring bean autowire自动装配

    转自:http://blog.csdn.net/xiao_jun_0820/article/details/7233139 autowire="byName"会自动装配属性与Bea ...

随机推荐

  1. Codeforces1114 D. Flood Fill (DP)(整个区间染成同色)

    题意:连续的几个颜色相同的格子称为一个连通块.选一个点为起点,每个操作是把所在连通块变一个颜色,求把整个区间染成同色需要的最少操作数.(注意,每次只能改变所在连通块的颜色,不能任选连通块,除了最开始时 ...

  2. 学习掌握oracle外表(external table)

    [转自] http://blog.chinaunix.net/uid-10697776-id-2935685.html 定义 External tables access data in extern ...

  3. once函数,简约不简单的

    module.exports = once once.proto = once(function () { Object.defineProperty(Function.prototype, 'onc ...

  4. 转帖 JS的基础语法2

    条件语句(if.switch). 循环语句(while.do…while. for … in).跳转语句(break,continue) 1.条件语句 Ø if语句 javascrip中的if语句 v ...

  5. 轻量级ORM框架:Dapper中的一些复杂操作和inner join应该注意的坑

    上一篇博文中我们快速的介绍了dapper的一些基本CURD操作,也是我们manipulate db不可或缺的最小单元,这一篇我们介绍下相对复杂 一点的操作,源码分析暂时就不在这里介绍了. 一:tabl ...

  6. (转)CentOS 7 sytemctl 自定义服务开机启动

    CentOS 7 sytemctl 自定义服务开机启动 原文:http://blog.csdn.net/ithomer/article/details/51766319 CentOS 7继承了RHEL ...

  7. HDU 5222 ——Exploration——————【并查集+拓扑排序判有向环】

    Exploration Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. flutter initializing gradle终极解决方案

    自己开发的公众号,可以领取淘宝内部优惠券 修改flutter.gradle文件 这种做法网上一大堆的教程,如果你还没改过建议先试下,比如这篇 Flutter 运行 一直Initializing gra ...

  9. 监控节点 xml写入数据库 存储过程

    USE [Appserver] GO /****** Object:  StoredProcedure [AppServer].[ImportNodeRelationData]    Script D ...

  10. C# url获取图片流转字符串

    //http url获取图片流转字符串 //string url = serverUrl.TrimEnd('/') + PUrl; //WebRequest request = WebRequest. ...