![](/img/trans.png)
[英]Spring MVC - Load bean definitions from appContext.xml programatically
[英]How to set parameter from *.properties into SPRING appContext.xml if i set location with JVM variable?
我有一个VM参数-Dapp.conf=/path/to/config.properties
,我有一个Spring 4.2.5应用程序的appContext.xml。 此config.properties
包含属性,例如database.username=username
在XML配置中,我有这个bean <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value= "${database.driver}"/> <property name="url" value="${database.url}"/> <property name="username" value="${database.username}"/> <property name="password" value="${database.password}"/> </bean>
我正在尝试使用以下方法读取我的配置文件:
`<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="location" value="file:///#{systemEnvironment['app.conf']}"/>
</bean>`
但是我插入的参数value= "${database.driver}
不能从文件中读取。
我如何将参数从属性文件插入到数据源?
在这种情况下,它只会插入${database.driver}
而我有例外,该参数无效。
在Spring BOOT中,我已经做到了这一点,并且很有效:
Properties properties = new Properties();
try (Reader reader =
new FileReader(
System.getProperty("app.conf")
//this contains path:"D://config.properties"
)) {
properties.load(reader);
} catch (IOException e) {
System.out.println(e.getMessage());
}
for (String propertyName: properties.stringPropertyNames()) {
System.setProperty(propertyName, properties.getProperty(propertyName));
}
该代码将我的属性作为VM参数加载,并且我可以使用Spring注释@Value("#{property.name}")
访问它们
我不知道为什么,但是System.setProperties(properties);
没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.