2016-08-18 7 views
0

キャメルスプリングアプリケーションをスプリングブート最新バージョン1.4.0に変換しています。スプリングブート - クラスパスとリスナーの外側にコンテキストXMLをロード

柔軟性を追加するために、私のアプリケーションコンテキストxmlが私のWARの外にあります。しかし、私はそれをプロジェクトにロードすることができません。プロジェクトのクラスパスの中に追加し、@ImportResourceを使用するとうまく動作します。どのように私はそれを外部からロードする。

私のweb.xml(古い)

<?xml version="1.0"?> 
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
          "http://java.sun.com/dtd/web-app_2_3.dtd"> 
    <web-app> 
    <display-name>Archetype Created Web Application</display-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>file:///${camel.config.location}</param-value> 
     <description> location of spring xml files</description> 
    </context-param> 
    <context-param> 
      <param-name>log4jConfigLocation</param-name> 
      <param-value>file:///${log4j.config.location}</param-value> 
    </context-param> 
    <context-param> 
     <param-name>log4jRefreshInterval</param-name> 
     <param-value>10000</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
      <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
     </listener> 
    </web-app> 

私は例を通して検索した後、以下のアプリケーションクラスを実装しているが、context.xmlにはロードされません。私はそれをリソースローダーとして追加しました。私はonStartupも使用しましたが、まだ気づいているのはスプリングブートロードだけです。

@Configuration 
    @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, 
      DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) 
    @ComponentScan 
    public class DLBootApplication extends SpringBootServletInitializer { 

     public static void main(String[] args) throws Exception { 
      final ConfigurableEnvironment environment = new StandardServletEnvironment(); 
      initialize(environment); 
      final ResourceLoader resourceLoader = createResourceLoader(); 
      final ApplicationContext ctx = new SpringApplicationBuilder(DLBootApplication.class) 
        .contextClass(AnnotationConfigEmbeddedWebApplicationContext.class).environment(environment) 
        .resourceLoader(resourceLoader).run(args); 

     } 

     public static void initialize(ConfigurableEnvironment environment) { 
      final String camelConfig = environment.getProperty(Constants.DL_CAMEL_CONFIG_LOCATION); 
      final String log4jConfig = environment.getProperty(Constants.DL_LOG4J_CONFIG_LOCATION); 


      if (StringUtils.isEmpty(camelConfig)) { 
       throw new IllegalStateException(Constants.DL_CAMEL_CONFIG_LOCATION + " is empty"); 
      } 
      if (StringUtils.isEmpty(log4jConfig)) { 
       throw new IllegalStateException(Constants.DL_LOG4J_CONFIG_LOCATION + " is empty"); 
      } 

      System.setProperty(Constants.DL_CAMEL_CONFIG_LOCATION, camelConfig); 
      System.setProperty(Constants.DL_LOG4J_CONFIG_LOCATION, log4jConfig); 

     } 

     public static ResourceLoader createResourceLoader() { 
      final FileSystemResourceLoader resLoader = new FileSystemResourceLoader(); 
      resLoader.getResource(System.getProperty(Constants.DL_CAMEL_CONFIG_LOCATION)); 
      return resLoader; 
     } 

     @Override 
     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
      final ConfigurableEnvironment environment = new StandardServletEnvironment(); 
      initialize(environment); 
      final ResourceLoader resourceLoader = createResourceLoader(); 
      return application.sources(DLBootApplication.class).environment(environment).resourceLoader(resourceLoader); 
     } 

     @Override 
     public void onStartup(ServletContext servletContext) throws ServletException { 
      // Create the 'root' Spring application context 
      final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
      rootContext.register(DLBootApplication.class); 

      // Manage the lifecycle of the root application context 
      servletContext.addListener(new ContextLoaderListener(rootContext)); 
      servletContext.addListener(new Log4jConfigListener()); 

     } 

    } 

答えて

0

@Importresorceと環境変数からのファイルの場所を使用しています。