如何在 Spring Boot 应用程序中设置系统属性 [英] how to set system property in spring boot application

查看:84
本文介绍了如何在 Spring Boot 应用程序中设置系统属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Spring Boot 应用程序中设置系统属性.我不想从命令行设置它.

I have a requirement to set system property in spring boot application. I don't want to set it from command line.

我关心的是最好的做法是什么.无论是从构造函数或者在 main 方法里面.下面是从构造函数设置它的示例

My concern is what is the best practice to do it. Either from constructor Or inside main method. Below is sample for setting it from constructor

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

}

什么是最好的方法?

推荐答案

从 Java 代码内部设置系统变量不是一个好主意.基本上,变量是为了让代码没有任何变量值.

Not good idea to set system variables from inside Java code. Basically, variables are meant to keep the code free from having any variable values.

使用属性文件来存储您的配置.Spring Boot 在外部化您的配置方面做得很好.它还可以让您在单独的文件中进行环境配置,并在初始化方面做得很好.

Use properties files to store your configurations. Spring Boot does a great job externalising your configurations. It also let to you have environmental configurations in separate files and does a great job initialising it.

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

这篇关于如何在 Spring Boot 应用程序中设置系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆