zoukankan      html  css  js  c++  java
  • 自动装配、JavaConfig、XML 三种方案之间,怎么导入和混合配置?

    在 Spring 中,这些配置方案都不是互斥的。完全可以将 JavaConfig 的组件扫描自动装配/或 XML 配置混合在一起。

    Q:如何在 JavaConfig 中引用 XML 配置?

    Q:怎么将两个 JavaConfig 类组合在一起?

     1 package soundsystem;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 
     6 @Configuration
     7 public class CDConfig {
     8 
     9     @Bean
    10     public CompactDisc compactDisc(){
    11         return  new SgtPeppers();
    12     }
    13 }
     1 package soundsystem;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 import org.springframework.context.annotation.Import;
     6 
     7 @Configuration
     8 @Import(CDConfig.class)
     9 public class CDPlayerConfig {
    10 
    11     @Bean
    12     public CDPlayer cdPlayer(CompactDisc compactDisc) {
    13         return new CDPlayer(compactDisc);
    14     }
    15 
    16 }

    ②、一种更好的方式:创建一个更高级别的 SoundSystemConfig,并在这个类中使用 @Import 将两个配置类组合在一起。

    1 package soundsystem;
    2 
    3 import org.springframework.context.annotation.Configuration;
    4 import org.springframework.context.annotation.Import;
    5 
    6 @Configuration
    7 @Import({CDPlayerConfig.class, CDConfig.class})
    8 public class SoundSystemConfig {
    9 }

    Q:该如何让 Spring 同时加载 通过 XML 来配置的 BlankDisc 和 其他基于 Java 的配置呢?
    A:@ImportResource 注解。假定 BlankDisc 定义在名为 cd-config.xml 文件中,该文件位于根类路径下,那么可以修改 SoundSystemConfig ,让它使用 @ImportResource 注解。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:c="http://www.springframework.org/schema/c"
     4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <bean id="compactDisc"
     8           class="soundsystem.BlankDisc"
     9           c:_0="titleValue"
    10           c:_1="artist">
    11         <constructor-arg>
    12             <list>
    13                 <value>tracks01</value>
    14                 <value>tracks02</value>
    15                 <value>tracks03</value>
    16             </list>
    17         </constructor-arg>
    18     </bean>
    19 </beans>

    Q:如何在 XML 配置中引用 JavaConfig?

    A:在 JavaConfig 配置中,展示了如何使用 @Import 和 @ImportResource 来拆分 JavaConfig 类。 
    在 XML 中,我们使用 import 元素来拆分 XML 配置。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:c="http://www.springframework.org/schema/c"
     4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <!--
     8         我们将 BlankDisc 配置在 JavaConfig中,CDPlayer 继续配置在 XML 中
     9         这时,<import>元素只能导入其他的 XML 配置文件,为了将 JavaConfig 类导入到 XML 配置中,我们使用 <bean> 元素。
    10      -->
    11     <bean class="soundsystem.CDConfig"/>
    12 
    13     <bean id="cdPlayer"
    14         class="soundsystem.CDPlayer"
    15         c:compactDisc-ref="compactDisc"/>
    16 </beans> 

    我们也可以创建一个更高层次的配置文件,这个文件不声明任何的bean,只是负责将两个或者更多的配置组合起来。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:c="http://www.springframework.org/schema/c"
     4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     6 
     7     <bean class="soundsystem.CDConfig"/>
     8 
     9     <import resource="cdplayer-config.xml"/>
    10 
    11 </beans>

    不管使用 JavaConfig 还是使用 XML 进行装配,我通常都会创建一个根配置,这个配置会将两个或更多的装配类和/或 XML 文件组合起来。 
    我也会在根配置中启用组件扫描(通过 context:component-scan 元素 或 @ComponentScan)。



  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/xiehang/p/9739290.html
Copyright © 2011-2022 走看看