Spring 实战 通过XML装配Bean

Spring从诞生之时就是使用XML来配置各个Bean。但是现在XML不再是配置的唯一选择,JavaConfig和AutoWired都是现在更加推荐的选择。这一章节主要是希望能够帮助我们维护现有的代码!!新写的代码还是比较推荐使用JavaConfig或者自动装配。

 2.4.1 创建XML配置规范 

以下就是一个标准的配置文件!<beans>标签内部可以声明所需要的Bean。用来装配Beans的最基本的XML元素包含在spring-beans模式中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="compactDisc" class="soundSystem.SgtPeppers"/>
</beans>

2.4.2 声明一个简单的<bean>

上面的那个配置中有声明一个Bean的<bean>标签,这个和@Bean的效果是一样的!class属性中放着这个bean的全限定名!如果没有添加id属性,则它的名字将会是soundSystem.SgtPeppers#0

使用如下的测试用例来测试!需要使用@ContextConfiguration注解来寻找classpath下的xml配置文件!

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:inner/springConfig.xml"})
public class CDPlayerTest {

    @Autowired
    private CompactDisc cd;

    @Test
    public void cdShouldNotBeNull(){
        assertNotNull(cd);
    }
}

自动化的命名很方便,但是如果这个Bean需要被注入到另外一个Bean当中,则我们需要使用id属性来为它命名!只对那些需要注入到其他Bean中的Bean命名!

我们不再负责创建SgtPeppers的实例,Spring发现这个<bean>元素的时候,它将会调用默认的构造器来创建Bean!

和JavaConfig相比的缺点:

1. 使用xml比较被动,也没有Javaconfig来得强大,在Javaconfig中我们可以使用任何可以想象得到的方法来创建Bean。

2. Spring的XML配置不能从编译期的类型检查中获益,没人能够保证class属性中的内容是正确的,如果你重构了这个类或者重命名了这个类名,就会引起一些问题。

2.4.3 借助构造器注入初始化Bean

在Spring XML配置中,使用<bean>元素并指定class属性。Spring会从这里获取必要的信息来创建Bean。

构造器注入有两种配置方式可以选择:

1. <constructor-arg>元素:繁琐但是能力强

2. c-命名空间:简单但是有些事情做不到

constructor 的方式如下,将compactDisc注入到了CDplayer当中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="compactDisc" class="soundSystem.SgtPeppers"/>
    <bean id="cdPlayer" class="soundSystem.CDPlayer">
        <constructor-arg ref="compactDisc"/>
    </bean>
</beans>

c-命名空间:需要添加c标签的引用,使用cd-ref来注入compactDisc。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="compactDisc" class="soundSystem.SgtPeppers"/>
    <bean id="cdPlayer" class="soundSystem.CDPlayer" c:cd-ref="compactDisc"/>
</beans>

c:cd-ref="compactDisc" 是怎么组成的

"c:"开头,命名空间前缀

"cd": 构造器的参数名字

”-ref",的意思是告诉spring,正在装配的是一个bean的引用compactDisc,而不是“compactDisc"这个字面量。(String)

这里直接使用了构造器参数的名称,引用参数名称有点怪异 如果改了名字就会报错?调试标志?是什么意思?

其他的引用方法:

只有一个构造器参数的时候使用
<bean id="cdPlayer" class="soundSystem.CDPlayer" c:_-ref="compactDisc"/>
参数索引,使用0代表第一个参数,因为标签不能以数字开头所有用一个下划线_开头
<bean id="cdPlayer" class="soundSystem.CDPlayer" c:_0-ref="compactDisc"/>

字面量

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="compactDisc" class="soundSystem.BlankDisc">
        <constructor-arg value="Sgt. Pepper's Lonely Hearts Club Banc"/>
        <constructor-arg value="The Beatles"/>
    </bean>
    <bean id="cdPlayer" class="soundSystem.CDPlayer" c:input-ref="compactDisc"/>
</beans>

我们之前的例子,compactDisc里面的东西是写死在类里面的,但是我们也可以使用字面量注入来更加灵活地配置类!!ref标签变成了value,就代表注入了字面量。

同样的我们也可以使用c标签!好像和书上有出入,书上有_, _artist, _title

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="compactDisc" class="soundSystem.BlankDisc"
          c:artist="The Beatles"
          c:title="Sgt. Pepper's Lonely Hearts Club Banc"/>
    <bean id="cdPlayer" class="soundSystem.CDPlayer" c:input-ref="compactDisc"/>
</beans>

目前来看,在装配bean引用和字面量值方面,构造器注入和c命名空间是相同的,但是却无法装配集合。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值