Aub*_*ine 29 java spring environment-variables amazon-web-services
在我们设置环境变量之后,AWS System.getProperty("JDBC_CONNECTION_STRING")在http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html中进行了讨论.一切都很好,除了我不能System.getProperty在我的Spring XML配置代码中调用,也不能调用资源包快捷方式,因为资源包本身必须以某种方式提取这些环境变量来为它们服务.请帮助我转换此示例配置以使用环境变量?:-)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://secrethost:007/whois?autoReconnect=true" />
<property name="username" value="bond" />
<property name="password" value="abuginsidemistycorner" />
<property name="initialSize" value="100" />
<property name="minEvictableIdleTimeMillis">
<value>300000</value>
</property>
<property name="timeBetweenEvictionRunsMillis">
<value>60000</value>
</property>
<property name="maxIdle" value="20" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我无法理解人们在这里做了什么:
我可以为Spring FileSystemResource使用基于环境变量的位置吗?哪个适用于最近的春季版?
M. *_*num 46
首先<context:property-placeholder .. />在配置中添加一个元素.
<context:property-placeholder />
Run Code Online (Sandbox Code Playgroud)
然后只需在配置中使用占位符.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${JDBC_CONNECTION_STRING}" />
<property name="username" value="bond" />
<property name="password" value="abuginsidemistycorner" />
<property name="initialSize" value="100" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="maxIdle" value="20" />
</bean>
Run Code Online (Sandbox Code Playgroud)
确保占位符名称与您设置的变量匹配.
如果您使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类来加载属性文件,则可以将该属性systemPropertiesMode设置为该值SYSTEM_PROPERTIES_MODE_OVERRIDE.
在spring.xml中你将拥有这个bean:
<bean id="propertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations">
<list>
<value>classpath://file.properties</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
Spring将以这种方式加载系统属性:
在尝试指定的属性之前,首先检查系统属性.这允许系统属性覆盖任何其他属性源.
通过这种方式,您应该能够将系统属性作为普通属性读取.
小智 5
对于使用JavaConfig的人:
在@Configuration文件中,我们需要:
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ClassPathResource[] resources = new ClassPathResource[ ] {
new ClassPathResource("db.properties")
};
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
ppc.setSearchSystemEnvironment(true);
return ppc;
}
@Value("${db.url}")
private String dbUrl;
@Value("${db.driver}")
private String dbDriver;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
@Bean
public DataSource db() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(dbUrl);
dataSource.setDriverClassName(dbDriver);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
Run Code Online (Sandbox Code Playgroud)
重要的是一行:ppc.setSearchSystemEnvironment(true);
之后在db.properties中,我有:
db.url = ${PG_URL}
db.driver = ${PG_DRIVER}
db.username = ${PG_USERNAME}
db.password = ${PG_PASSWORD}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
61257 次 |
| 最近记录: |