代码之家  ›  专栏  ›  技术社区  ›  KRR16

如何在spring boot应用程序中设置系统属性

  •  1
  • KRR16  · 技术社区  · 6 年前

    我需要在spring boot应用程序中设置系统属性。 我不想从命令行设置它。

    来自构造函数

    @SpringBootApplication
    class Sample{
    @Autowired
    protected TempInfoDao tempInfoDao;
    
    public Sample{
       //Setting System property inside constructor
        System.setProperty("vertx.hazelcast.config","./config/cluster.xml");
    }
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(Sample.class, args);
    }
    

    什么是最好的方法?

    2 回复  |  直到 6 年前
        1
  •  2
  •   Amit Phaltankar    6 年前

    从Java代码内部设置系统变量不是个好主意。 基本上,变量是为了使代码不包含任何变量值。

    使用属性文件存储配置。SpringBoot在外部化配置方面做得很好。

    参考 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

        2
  •  0
  •   Alien    6 年前

    在构造函数中设置系统属性不是一个好方法。

    @Profile("production")
    @Component
    public class ProductionPropertySetter {
        @PostConstruct
        public void setProperty() {
           System.setProperty("http.maxConnections", 15);
        }
    }