Spring_总结_03_装配Bean(四)_导入与混合配置

一、前言

本文承接上一节:Spring_总结_03_装配Bean(三)之XML配置

在典型的Spring应用中,我们可能会同时使用自动化和显示配置。同时,可能在某些场景下我们需要混合使用JavaConfig和xml配置。

 

二、在JavaConfig中引用XML配置

(1)可使用 @import注解导入JavaConfig

假设我们的配置类已经很笨重了,这时,我们可以将配置进行拆分。用一个高级别配置来组合其他配置

如:现在又两个配置类:CDConfig、CDPlayerConfig  以及一个高级配置SoundSystemConfig

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

 

若想将CDconfig用xml形式配置,则引入的时候需要使用

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

 

三、在XML中引用JavaConfig

 

在一个高级别XML配置中同时引入JavaConfig和XMLConfig

 

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

    <!-- 1.通过  bean 引入JavaConfig -->
    <bean  class="soundsystem.CDConfig" />

    <!-- 2.通过 import 引入其他xml配置 -->
    <import resource="cdplayer-config.xml" />
    
</beans>

 

转载于:https://www.cnblogs.com/shirui/p/9383332.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值