【问题标题】:SpringBoot and use / load a dynamic "applicationContext.xml" instead of hard coded oneSpringBoot 并使用/加载动态的“applicationContext.xml”而不是硬编码的
【发布时间】:2023-04-11 00:48:01
【问题描述】:

我见过一百个这样的例子:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

现在我已经在兔子小道上走了好几个小时了。

我正在构建一个框架......并且我需要从 xml 依赖注入文件(又名“beans”)中加载(少数依赖项,而不是全部......):

applicationContext.xml

我需要命名为动态的,而不是硬编码的。

String myValue = "DefaultEnvVarValue";
String envValue = System.getenv("MYENVVARIABLENAME");
if (null != envValue )
{
     myValue=envValue;
}
String topLevelAppContextFileName = "applicationContext." + myValue + ".xml";

没有springboot,我会这样做:

 ApplicationContext context = new ClassPathXmlApplicationContext(topLevelAppContextFileName);

有没有办法用 SpringBoot 解决这个问题?

我找到了属性文件的 PropertySourcesPlaceholderConfigurer,但找不到任何依赖注入的东西。

旁注:

在我收到“xml bad”评论之前,我的大部分依赖项都是基于注释的。但我正在制作一个供其他人使用的框架,因此我需要其中一些是 xml 驱动的.....也就是说,我有正当理由让一些 DI 是 xml 驱动的。

【问题讨论】:

    标签: spring-boot spring-bean


    【解决方案1】:

    这可以工作 -

    配置

    public class DemoApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    
         @Override
         public void initialize(ConfigurableApplicationContext ac) {
          ac = new ClassPathXmlApplicationContext(topLevelAppContextFileName);
         }
    }
    

    主要

        public static void main(String args[]) {
             new SpringApplicationBuilder(Application.class)
                .initializers(new DemoApplicationContextInitializer())
                .run(
        }
    

    【讨论】:

      【解决方案2】:

      对于未来的读者,我最终这样做了:

      @SpringBootApplication
      @ImportResource({"classpath*:applicationContext.xml"})
      
      public class MySpringBootApplication { 
      
      
          public static void main(String[] args) {
              try {
      
                  URL resource = MySpringBootApplication.class.getResource("/applicationContext.xml");
                  if (null == resource || StringUtils.isBlank(resource.getPath())) {
                      throw new FileNotFoundException("applicationContext.xml not found.  The entry dependency injection file must be applicationContext.xml");
                  }
      
                  org.springframework.context.ConfigurableApplicationContext applicationContext = SpringApplication.run(MySpringBootApplication.class, args);
      

      然后我将“动态”部分放在里面的 applicationContext.xml 文件中。

      注意“:”分隔符,如果环境变量不存在,它将允许使用默认值。

      <?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:context="http://www.springframework.org/schema/context"
             xmlns:util="http://www.springframework.org/schema/util"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util.xsd">
      
          <import resource="projectContext.${MYENVVARIABLENAME:DefaultEnvVarValue}.xml"/>
      

      这实现起来更简单,尽管从技术上讲,我有 2 个文件,而不是 1 个。

      所以如果环境变量不存在,会默认导入第二个名为:

      projectContext.DefaultEnvVarValue.xml

      【讨论】:

        猜你喜欢
        • 2020-07-13
        • 1970-01-01
        • 1970-01-01
        • 2013-02-07
        • 1970-01-01
        • 2023-03-16
        • 2017-08-16
        • 2014-01-14
        • 1970-01-01
        相关资源
        最近更新 更多