2.5导入和混合配置 P61 Spring实战 第四版

导入和混合配置:

在典型的spring应用当中,我们可能同时使用自动化和显示配置。(java config/ xml配置) 各有优势!!

spring并不排斥这些配置方案,我们可以将java config/ 自动装配 autowired/ xml配置混合在一起,只需要一些显示配置来启动组件扫描和自动装配。

2.5.1在java config中引用xml配置

1. 拆分CDconfig

@Configuration
public class CDConfig {
    @Bean
    public CompactDisc compactDisc(){
        return new SgtPeppers();
    }
}

@Configuration
//@ComponentScan
@Import(CDConfig.class)
public class CDPlayerConfig {

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

在使用JavaConfig的时候,有的时候我们需要导入另外一个configuration类,那么我们就使用@Import来导入

2. 使用一个配置类专门来聚合

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

3. 导入 XML 配置的 Bean

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

2.5.2在xml配置中引用JavaConfig

1. 拆分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"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">
    <bean id="compactDisc" class="soundSystem.BlankDisc" p:tracks-ref="trackList">
        <property name="title" value="Sgt. Pepper's"/>
        <property name="artist" value="The Beatles"/>
    </bean>

    <util:list id="trackList">
        <value>Sgt. Pepper</value>
        <value>with a little help</value>
    </util:list>
</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"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util
                           http://www.springframework.org/schema/util/spring-util.xsd">
    <import resource="cdConfig.xml"/>
    <bean id="cdPlayer" class="soundSystem.CDPlayer" p:cd-ref="compactDisc"/>
</beans>

我们使用<import>标签来导入另外一个 xml 配置文件。

2. 将java config 导入 xml 配置文件中

    <bean class="soundSystem.CDConfig"/>
    <bean id="cdPlayer" class="soundSystem.CDPlayer" p:cd-ref="compactDisc"/>

使用bean这个标签来导入配置的javaconfig,这种方式idea 似乎无法正确识别,强迫症患者不建议使用!!!

总结:

建议搞一个根配置文件,然后把所有的子配置文件导进来,然后开启组件扫描之类的东西!!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值