Spring Boot 我可以在Sping Boot 配置文件中定义系统属性吗?

mgdq6dx1  于 7个月前  发布在  Spring
关注(0)|答案(5)|浏览(118)

我的Sping Boot 应用程序只有一个application.yml配置文件,它定义了两个配置文件(如文档中所述)。
当启用生产配置文件时,我想将http.maxConnections系统属性设置为自定义值,例如。

spring:
    profiles:
        active: dev
---
spring:
    profiles: dev
---
spring:
    profiles: production
http:
    maxConnections: 15

字符串
但这实际上并没有设置系统级属性;它似乎只是创建了一个应用程序级属性。

java -jar -Dspring.profiles.active=production myapp.jar


java -Dhttp.maxConnections=15 myapp.jar


我想我可以在“生产”配置文件上创建一个@Conditional bean,它基于我的application.yml定义的属性以编程方式调用System.setProperty,但是是否有一种更简单的方法只通过配置文件?

8nuwlpux

8nuwlpux1#

你可以试试

@Profile("production")
@Component
public class ProductionPropertySetter {

   @PostConstruct
   public void setProperty() {
      System.setProperty("http.maxConnections", "15");
   }

}

字符串

chy5wohz

chy5wohz2#

我想我可以在“生产”配置文件上创建一个@Conditional的bean,它基于我的application. yml定义的属性以编程方式调用System.setProperty,但是是否有一种更简单的方法只通过配置文件?
我认为这是最好的选择。Sping Boot 在其LoggingSystem中自己完成了这一点,其中各种logging.*属性Map到System属性。
请注意,您可能希望尽可能早地设置系统属性,可能是在Environment准备好之后。为此,您可以使用ApplicationListener侦听ApplicationEnvironmentPreparedEvent。您的ApplicationListener实现应该通过spring.factories中的条目注册。

qzwqbdag

qzwqbdag3#

您可以将环境注入到指定Bean的类的构造函数中。这允许您在创建Bean之前将应用程序属性写入系统属性。

@Configuration
public class ApplicationBeans {

   @Autowired
   public ApplicationBeans(Environment environment) {
      // set system properties before the beans are being created.
      String property = "com.application.property";
      System.getProperties().setProperty(property, environment.getProperty(property));
   }

   /**
    * Bean that depends on the system properties
    */
   @Bean
   public SomeBean someBean() {
      return new SomeBean();
   }
}

字符串

gab6jxml

gab6jxml4#

我遇到了类似的挑战,并努力寻找一种可靠的方法来使用Spring配置文件外部化系统属性。虽然命令行参数是一种选择,但我更倾向于避免这种方法,以便将所有配置设置整合在一个地方。
您可以在项目中使用以下类:

import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(
        prefix = "system")
@Lazy(
        value = false)
public class SystemPropertiesInjector {

    private static final Logger LOGGER = LoggerFactory.getLogger(SystemPropertiesInjector.class);

    Map<String, String> properties;

    @PostConstruct
    void init() {
        if (properties != null) {

            for (Entry<String, String> property : properties.entrySet()) {
                LOGGER.info("Injecting Property with Name: {} and Value: {} into System Properties",
                        property.getKey(),
                        property.getValue());
                System.setProperty(property.getKey(), property.getValue());
            }
        }
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
    }

}

字符串
在application.properties中:

system.properties.propertyName=propertyValue


该组件从Spring Context中读取前缀为“system.properties“的所有属性,并将其放入System#setProperty中。当然,您可以根据需要更改前缀。

beq87vna

beq87vna5#

您还可以使用org.springframework.beans.factory.config中的PropertyPlaceholderConfigurer来获取属性文件的句柄

相关问题