根据this answer,可以使用Spring批处理类org.springframework.batch.support.SystemPropertyInitializer
在启动Spring上下文时设置系统属性。使用SystemPropertyInitializer在设置属性占位符前设置系统属性
特别,我希望能够用它来设置ENVIRONMENT
因为Spring Batch的配置的部分内容:
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
<value>classpath:batch-default.properties</value>
<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
</list>
</property>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="order" value="1" />
</bean>
但SystemPropertyInitializer
使用afterPropertiesSet()
来设置系统属性,显然这种情况后PropertyPlaceholderConfigurer
的配置。
有没有可能做到这一点?
我不确定系统属性和环境属性之间的差异是多么适用。类SystemPropertyInitializer在其代码中调用System.setProperty()。我遇到的是它在* placeholderProperties解析后才会被调用。这个解决方案不会遇到同样的问题吗?我对Spring的生命周期知识还不够自信。 – Stewart
上面增加了详细信息(太长的评论) –