Spring实战(4):装配Bean——导入和混合配置

时间:2022-07-12 20:31:16

在典型的Spring应用中,我们可能会同时使用自动化和显式配置。就产生了混合配置,即可以在JavaConfig中引入XML配置,也可以在XML配置中引用JavaConfig。

在JavaConfig中引用XML配置

之前我们在JavaConfig配置中写过一个CDPlayerConfig:

2.27:CDPlayerConfig

@Configuration
public class CDPlayerConfig {

    @Bean
    public CompactDisc taylorSwift(){
        return new TaylorSwift();
    }

    @Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc){
        return new CDPlayer(compactDisc);
    }
}

在上面地代码中,我们将CompactDisc Bean和CDPlayer Bean装配在了一个JavaConfig中, 现在,我们觉得应该将CompactDisc类型的Bean和CDPlayer类型的Bean应该分开配置,所以要将CompactDisc Bean从CDPlayerConfig中拆分出来。像下面这样:

2.28:将CompactDisc Bean从CDPlayerConfig中拆分

@Configuration
public class CDConfig {
    @Bean
    public CompactDisc taylorSwift(){
        return new TaylorSwift();
    }
}

此时我们需要一种方法将这两个Bean装配在一起,我们可以在CDPlayerConfig中使用@Import将CDConfig导入。如下:

2.29:在CDPlayerConfig中使用@Import

@Configuration
@Import(CDConfig.class)

public class CDPlayerConfig {

    @Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc){
        return new CDPlayer(compactDisc);
    }
}

或者如果不想在CDPlayerConfig中使用@Import标注,我们可以再重新创建一个Config。将这两个Config都导入,如下:

2.29:SoundSystemConfig

@Configuration
@Import({CDPlayerConfig.class, CDConfig.class})
public class SoundSystemConfig {
}

不管采用哪种方式,我们都将CDPlayer的配置与CompactDisc的配置分开了。

现在,我们假设希望通过XML来配置CompactDisc:

2.29:一个简单的XML配置文件CDMix.xml

<?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="taylorSwift" class="com.soundsystemMix.Taylor" />

</beans>

现在CompactDisc配置在了XML中,我们该如何让Spring同时加载它和其他基于Java的配置呢?
答案是@ImportResource注解。

2.30:导入CDMix.xml和CDPlayerConfig的SoundSystemConfig

@Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:CDMix.xml")
public class SoundSystemConfig {
}

此时,两个Bean——配置在JavaConfig中的CDPlayer以及配置在XML中的CompactDisc都会被加载到Spring容器之中。因为CDPlayer中带有@Bean注解的方法接受一个CompactDisc作为参数,因此CompactDisc将会被装配进来,此时与它是通过XML配置的没有任何关系。


在XML配置中引用JavaConfig

我们在JavaConfig中引用另外一个JavaConfig或者XML文件,使用的是@Import注解,那么在XML配置文件,引用另外一个XML配置文件或JavaConfig需要用到< import>元素。

2.31:在XML配置文件中引用另一个XML文件

<?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">

    <import resoune="CDMix.xml" />
    <bean id="cdPlayer" class="com.soundsystemMix.CDPlayer" c:cd-ref="taylorSwift" />

</beans>

现在,我们要将CompactDisc Bean配置在JavaConfig中,CDPlayer则继续配置在XML中。

在XML配置文件中引用JavaConfig与在XML中引用XML还略微有一点不同。
在XML中引用JavaConfig要将引入的JavaConfig声明为一个Bean,代码如下:

2.32:在XML中引用JavaConfig

<?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 class="com.soundsystemMix.CDPlayer" />
    <bean id="cdPlayer" class="com.soundsystemMix.CDPlayer" c:cd-ref="taylorSwift" />

</beans>

采用这样的方式,两种配置——一个使用XML描述,一个使用Java描述被组合在了一起。

也可以把CDConfig Bean从之前的XML中移除,使用第三个配置文件将这两个组合在一起。