spring boot - 在classpath和listeners之外加载context xml

时间:2016-08-18 08:32:13

标签: spring-boot

我正在将我的camel spring应用程序转换为使用spring boot最新版本1.4.0。

我的应用程序上下文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());

        }

    }

1 个答案:

答案 0 :(得分:0)

使用@Importresorce和环境变量中的文件位置。