2016-08-18 58 views
0

我正在將我的駱駝春應用程序轉換爲使用spring啓動最新版本1.4.0。spring boot - 在classpath和listeners外部加載上下文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和環境變量中的文件位置。

相關問題