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

最小化Spring XML配置

时间:2016-07-08 19:34:39      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

 

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

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

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

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

  4 autodetect 首先尝试使用constructor经行自动装配。如果失败,再次尝试使用byType经行自动装配。(这个测试了,3.0.5版本不可用了,不知道是不是被移除了。)

 

 采用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="25" />
     </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="25" />
     </bean>
</beans>

 

 

最小化Spring XML配置

标签:

原文地址:http://www.cnblogs.com/bmbi/p/5654223.html

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