如何在 spring 启动应用程序中设置系统 属性
how to set system property in spring boot application
我需要在 spring 启动应用程序中设置系统 属性。
我不想从命令行设置它。
我担心的是最佳做法是什么。
来自构造函数
或者在 main 方法里面。下面是从构造函数
设置它的示例
@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 代码中设置系统变量不是个好主意。
基本上,变量是为了让代码不包含任何变量值。
使用属性文件来存储您的配置。 Spring Boot 可以很好地外化您的配置。
它还可以让您在单独的文件中进行环境配置,并且可以很好地初始化它。
参考https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
在构造函数中设置系统属性不是一个好方法。
您可以使用单独的 class 和 spring 注释来执行此操作,如下所示。
@Profile("production")
@Component
public class ProductionPropertySetter {
@PostConstruct
public void setProperty() {
System.setProperty("http.maxConnections", 15);
}
}
您的示例 class 应该基于您编写的特殊 class。我建议名称 BaseSettingProperties
public class TestBaseWithProperties extends AbstractTestNGSpringContextTests {
{
System.setProperty("name.of.property", "value/of/property");
}
}
这样您可以保证在之前真正设置属性,所有这些都阅读上下文和连接。您甚至可以在 include
d XML 中使用这些属性。
可以在 bean 中设置变量,方法是将属性放入某些 file.of.needed.properties 并使用它
<bean id="prop" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="file.of.needed.properties" />
</bean>
,但不能保证属性设置和include
调用的先后顺序。因为它不是呼唤而是物理包含。并且您不能在属性设置 bean 上设置 include
的依赖性 - 我没有发现该语法:-(。另一方面,是的,我使用非常旧的第 3 版 Spring,但我最近在网上找不到解决办法
我需要在 spring 启动应用程序中设置系统 属性。 我不想从命令行设置它。
我担心的是最佳做法是什么。 来自构造函数 或者在 main 方法里面。下面是从构造函数
设置它的示例@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 代码中设置系统变量不是个好主意。 基本上,变量是为了让代码不包含任何变量值。
使用属性文件来存储您的配置。 Spring Boot 可以很好地外化您的配置。 它还可以让您在单独的文件中进行环境配置,并且可以很好地初始化它。
参考https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
在构造函数中设置系统属性不是一个好方法。
您可以使用单独的 class 和 spring 注释来执行此操作,如下所示。
@Profile("production")
@Component
public class ProductionPropertySetter {
@PostConstruct
public void setProperty() {
System.setProperty("http.maxConnections", 15);
}
}
您的示例 class 应该基于您编写的特殊 class。我建议名称 BaseSettingProperties
public class TestBaseWithProperties extends AbstractTestNGSpringContextTests {
{
System.setProperty("name.of.property", "value/of/property");
}
}
这样您可以保证在之前真正设置属性,所有这些都阅读上下文和连接。您甚至可以在 include
d XML 中使用这些属性。
可以在 bean 中设置变量,方法是将属性放入某些 file.of.needed.properties 并使用它
<bean id="prop" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="file.of.needed.properties" />
</bean>
,但不能保证属性设置和include
调用的先后顺序。因为它不是呼唤而是物理包含。并且您不能在属性设置 bean 上设置 include
的依赖性 - 我没有发现该语法:-(。另一方面,是的,我使用非常旧的第 3 版 Spring,但我最近在网上找不到解决办法