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>

 

posted @ 2018-07-28 20:37  shirayner  阅读(283)  评论(0编辑  收藏  举报