本篇介绍一下自动装配的知识,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。

最新文章

  1. Socket简单使用
  2. ZBrush中该如何调节多个SubTool
  3. ios 缺少合规证明
  4. JavaWeb 学习003-简单登录页面功能实现
  5. 深入理解JavaScript中的==运算符
  6. IOS中Json解析的四种方法
  7. Reverse Words in a String
  8. loadrunner解决在项目中的难点解决
  9. 40.扑克牌的顺子[Continuous cards]
  10. Data Flow -&gt;&gt; Pivot
  11. C语言高效编程的几招(绝对实用,绝对经典)
  12. noip2015运输计划
  13. delphi SysErrorMessage 函数和系统错误信息表 good
  14. canvas粒子时钟
  15. Lettuce_webdriver 自动化测试
  16. TCP/IP(八)之总结ICP/IP四层模型
  17. 利用Python循环(包括while&amp;for)各种打印九九乘法表
  18. 如何反编译APK?
  19. MySQL Packets larger than max_allowed_packet are not allowed
  20. Spvmn测试环境搭建及其安全性讨论

热门文章

  1. UVA - 10817 状压DP
  2. HihoCoder - 1048 状压DP 经典题
  3. linux下安装使用虚拟环境
  4. java中获得对象的方法
  5. SpringBoot 思维导图
  6. Python-2.7 配置 tab 自动补全功能
  7. Apache Beam的基本概念
  8. Fastjson解析多级泛型的几种方式&mdash;使用class文件来解析多级泛型
  9. C++11并发编程:async,future,packaged_task,promise
  10. ActivityGroup和TabActiviy的差异性?