Spring 之混合配置

【JavaConfig 导入另外一个 JavaConfig & JavaConfig 导入 XML】

package soundsystem.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

@Configuration
@Import(CDPlayerConfig.class)
@ImportResource("classpath:cd-config.xml")
public class SoundSystemConfig {
    // 没写 @Configuration 注解 貌似也可以
    // 在 IDEA 下,使用 @ImportResource 所导入的资源(.xml)必须放在 特定类型的文件夹下
}

 貌似有没有都可以正常读到数据

 

【XML 导入另外一个 XML & 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" 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">

    <import resource="cd-config.xml" />

    <bean id="cdPlayer"
          class="soundsystem.bean.CDPlayer"
          c:cd-ref="compactDisc" />

    <!-- 并没有 XML 元素可以直接导入 JavaConfig 类  -->
    <!-- 不过可以用 JavaConfig 类来声明 bean -->
    <bean class="soundsystem.config.CDConfig" />

    <!-- 可以创建一个高层次的配置文件,不声明任何 bean ,只是负责将配置文件组合起来 -->
    <!-- 它被称之为根配置 root configuration -->
</beans>
原文地址:https://www.cnblogs.com/xkxf/p/7150168.html