Java spring引导:如何动态设置spring属性

Java spring引导:如何动态设置spring属性,java,spring,spring-boot,properties,Java,Spring,Spring Boot,Properties,有很多属性可以在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/keysto

有很多属性可以在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中是如何工作的


如有任何帮助,我们将不胜感激。

您需要定义并注册如下应用程序侦听器:

public class DynamicPropertiesListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
  public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    // modify the properties here, see the ConfigurableEnvironment javadoc
  }
}
@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);
    }
}
或者,更好的做法是,使用以下内容创建
src\main\resources\META-INF\spring.factories
文件:

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

其中
x.y
是侦听器的包。

因为您知道在spring boot应用程序中启用SSL的属性。您可以在spring引导应用程序中以编程方式传递这些属性,如下所示:

public class DynamicPropertiesListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
  public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    // modify the properties here, see the ConfigurableEnvironment javadoc
  }
}
@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
类向应用程序添加属性


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

在Spring Boot中,您可以使用
System.getProperty(String key)
System.setProperty(String key,String value)

注意


这将覆盖静态属性(不是所有的,而是覆盖的)。

这仍然会考虑我在Apple?属性中定义的其他属性吗?是的。它将考虑您在属性文件中添加的其他属性。工作100%!!而且简单,无需修改spring boot的xxxxxx应用程序runner类