简体   繁体   English

spring 启动:如何动态设置 spring 属性

[英]spring boot : How to set spring properties dynamically

There are so many properties which can be defined in application.properties of spring boot application.在 spring 启动应用程序的 application.properties 中可以定义很多属性。

But I want to pass properties to configure ssl to spring from inside the code.但我想从代码内部传递属性以将 ssl 配置为 spring。

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

So as these will be spring defined properties to be used from application.properties.因此,这些将是 spring 定义的要从 application.properties 使用的属性。 I just want to set them from the code based on some logic.我只想根据一些逻辑从代码中设置它们。

For non spring application, I still have some idea that they can be passed as application, session or context properties but I am not aware on how this works in spring.对于非 spring 应用程序,我仍然知道它们可以作为应用程序、session 或上下文属性传递,但我不知道这在 Z2A2D595E6ED9A0B24F027F2B63B134D 中是如何工作的。

Any help would be appreciated.任何帮助,将不胜感激。

You need to define and register an ApplicationListener like this:您需要像这样定义和注册一个 ApplicationListener:

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

Now register the listener.现在注册监听器。 If you run the application with the main method:如果您使用main方法运行应用程序:

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

or, even better, create src\main\resources\META-INF\spring.factories file with this content:或者,更好的是,使用以下内容创建src\main\resources\META-INF\spring.factories文件:

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

Where xy is the package of your listener.其中xy是您的听众的 package。

Since you know the properties to enable SSL in the spring boot app.由于您知道在 spring 启动应用程序中启用 SSL 的属性。 You can pass these properties programmatically in your spring boot application like this:您可以在 spring 引导应用程序中以编程方式传递这些属性,如下所示:

@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);
    }
}

As you can see I have commented out this: SpringApplication.run(SpringBootTestApplication.class, args);如您所见,我已对此进行了注释: SpringApplication.run(SpringBootTestApplication.class, args); and used SpringApplicationBuilder class to add properties to the app.并使用SpringApplicationBuilder class 向应用程序添加属性。

Now, these properties are defined in the program, You can apply condition and change property values based on your requirements.现在,这些属性已在程序中定义,您可以根据需要应用条件并更改属性值。

In Spring Boot, you can read and edit propeties using System.getProperty(String key) and System.setProperty(String key, String value)在 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);
}

Note :注意

This will overwrite the static properties (not all but the overwritten ones).这将覆盖 static 属性(不是所有属性,而是被覆盖的属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM