本篇介绍一下自动装配的知识,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. 洛谷 P2482 loj #2885 [SDOI2010]猪国杀 题解【模拟】【贪心】【搜索】

    好玩的模拟题. 以后要经常写模拟题鸭 题目描述 游戏背景 <猪国杀>是一种多猪牌类回合制游戏,一共有\(3\)种角色:主猪,忠猪,反猪.每局游戏主猪有且只有\(1\)只,忠猪和反猪可以有多 ...

  2. Jquery动态绑定事件处理函数 bind / on / delegate

    1.bind方法绑定的事件处理函数不会应用到后来添加到DOM中的新元素.比如你在用bind给页面元素绑定事件之后,又新添加了一些与之前绑定过事件的元素一样的DOM元素,但是这些事件并不能在新的DOM元 ...

  3. UVA - 11795 状压DP

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  4. 通过 flume 上传数据到hive

    目标: 通过接受 1084端口的http请求信息, 存储到 hive数据库中, osgi为hive中创建的数据库名称 periodic_report6 为创建的数据表, flume配置如下: a1.s ...

  5. Java static{}语句块详解

    [转自] http://blog.csdn.net/lubiaopan/article/details/4802430 static{}(即static块),会在类被加载的时候执行且仅会被执行一次,一 ...

  6. Two Sum [easy] (Python)

    由于题目说了有且只有唯一解,可以考虑两遍扫描求解:第一遍扫描原数组,将所有的数重新存放到一个dict中,该dict以原数组中的值为键,原数组中的下标为值:第二遍扫描原数组,对于每个数nums[i]查看 ...

  7. 利用Flume将本地文件数据中收集到HDFS

    1. 创建文件 放入一个txt文件 然后查看hdfs上的文件夹 不知道为什么并没有出现本地的文件 也不报错 后来发现,没有在logs文件夹下面,在newlogs文件夹下面

  8. [Modelsim] 仿真的基本操作

    切换路径,建立库并编译所有源文件之后, 键入命令: vopt +acc topmodulename -o top vsim top 其中topmodulename是顶层模块的名称.

  9. React.js 小书 Lesson13 - 渲染列表数据

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson13 转载请注明出处,保留原文链接和作者信息. 列表数据在前端非常常见,我们经常要处理这种类型 ...

  10. 用js语句控制css样式

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...