【问题标题】:spring boot : How to set spring properties dynamicallyspring boot:如何动态设置弹簧属性
【发布时间】:2020-02-16 01:16:59
【问题描述】:

在spring boot应用的application.properties中可以定义很多属性。

但我想传递属性来配置 ssl 以从代码内部弹出。

server.ssl.enabled=true
# The format used for the keystore 
server.ssl.key-store-type=PKCS12
# The path to the keystore containing the certificate
server.ssl.key-store=keys/keystore.jks
# The password used to generate the certificate
server.ssl.key-store-password=changeit
# The alias mapped to the certificate
server.ssl.key-alias=tomcat

因此,这些将是从 application.properties 中使用的 spring 定义的属性。我只是想根据一些逻辑从代码中设置它们。

对于非 spring 应用程序,我仍然有一些想法,它们可以作为应用程序、会话或上下文属性传递,但我不知道它在 spring 中是如何工作的。

任何帮助将不胜感激。

【问题讨论】:

    标签: java spring spring-boot properties


    【解决方案1】:

    你需要像这样定义和注册一个ApplicationListener:

    public class DynamicPropertiesListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
      public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        ConfigurableEnvironment environment = event.getEnvironment();
        // modify the properties here, see the ConfigurableEnvironment javadoc
      }
    }
    

    现在注册监听器。如果您使用main 方法运行应用程序:

    SpringApplication app = new SpringApplication(primarySources);
    app.addListeners(new DynamicPropertiesListener());
    app.run(args);
    

    或者,更好的是,使用以下内容创建 src\main\resources\META-INF\spring.factories 文件:

    org.springframework.context.ApplicationListener=x.y.DynamicPropertiesListener
    

    x.y 是你的监听器的包。

    【讨论】:

      【解决方案2】:

      既然您知道在 Spring Boot 应用程序中启用 SSL 的属性。您可以像这样在 Spring Boot 应用程序中以编程方式传递这些属性:

      @SpringBootApplication
      public class SpringBootTestApplication {
          public static void main(String[] args) {
      
      //      SpringApplication.run(SpringBootTestApplication.class, args);
      
              Properties props = new Properties();
              props.put("server.ssl.key-store", "/home/ajit-soman/Desktop/test/keystore.p12");
              props.put("server.ssl.key-store-password", "123456");
              props.put("server.ssl.key-store-type", "PKCS12");
              props.put("server.ssl.key-alias", "tomcat");
      
              new SpringApplicationBuilder(SpringBootTestApplication.class)
                  .properties(props).run(args);
          }
      }
      

      如您所见,我已将其注释掉: SpringApplication.run(SpringBootTestApplication.class, args); 并使用SpringApplicationBuilder 类向应用程序添加属性。

      现在,这些属性已在程序中定义,您可以根据需要应用条件并更改属性值。

      【讨论】:

      • 这还会考虑我在 application.properties 中定义的其他属性吗?
      • 是的。它将考虑您在属性文件中添加的其他属性。
      【解决方案3】:

      在 Spring Boot 中,您可以使用 System.getProperty(String key)System.setProperty(String key, String value) 读取和编辑属性

      public static void main(String[] args) {
      
          // set properties
          System.setProperty("server.ssl.enabled", "true");
          System.setProperty("server.ssl.key-store-type", "PKCS12");
          System.setProperty("server.ssl.key-store", "keys/keystore.jks");
          System.setProperty("server.ssl.key-store-password", "changeit");
          System.setProperty("server.ssl.key-alias", "tomcat");
      
          // run
          SpringApplication.run(DemoApplication.class, args);
      }
      

      注意

      这将覆盖静态属性(除了被覆盖的之外,不是所有的)。

      【讨论】:

      • 工作 100% !!且简单无需修改spring boot的xxxxxxAplication runner类
      猜你喜欢
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2019-08-20
      • 2015-04-29
      • 1970-01-01
      • 2021-05-16
      • 2019-10-21
      • 2018-01-16
      相关资源
      最近更新 更多