简体   繁体   English

如果我使用JVM变量设置位置,如何将参数从* .properties设置到SPRING appContext.xml中?

[英]How to set parameter from *.properties into SPRING appContext.xml if i set location with JVM variable?

I have a VM parameter -Dapp.conf=/path/to/config.properties and i have a appContext.xml for my Spring 4.2.5 application. 我有一个VM参数-Dapp.conf=/path/to/config.properties ,我有一个Spring 4.2.5应用程序的appContext.xml。 This config.properties contains propertis like database.username=username config.properties包含属性,例如database.username=username

in the XML config i have this 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> 在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>

i'm trying to read my config files using this: 我正在尝试使用以下方法读取我的配置文件:

 `<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
    <property name="location" value="file:///#{systemEnvironment['app.conf']}"/>
</bean>`

But my inserted parameters value= "${database.driver} not reading from file. 但是我插入的参数value= "${database.driver}不能从文件中读取。

How i may insert my parameters from my properties file to a my DataSource? 我如何将参数从属性文件插入到数据源?

in this case its just inserts ${database.driver} and i have exception, that parameter is invalid. 在这种情况下,它只会插入${database.driver}而我有例外,该参数无效。

in Spring BOOT i've made this and its worked: 在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));
    }

this code loads my properties as VM parameters, and i can access them with Spring annotation @Value("#{property.name}") 该代码将我的属性作为VM参数加载,并且我可以使用Spring注释@Value("#{property.name}")访问它们

I don't know why, but System.setProperties(properties); 我不知道为什么,但是System.setProperties(properties); not worked. 没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC-以编程方式从appContext.xml加载bean定义 - Spring MVC - Load bean definitions from appContext.xml programatically 如何使用Spring-Boot导入appContext.xml? - How to import appContext.xml with Spring-Boot? 在Spring MVC appcontext.xml文件中配置两个mongodb实例? - configure two mongodb instances in Spring MVC appcontext.xml file? 依赖jar中的spring appContext.xml无法加载属性文件。 - spring appContext.xml in dependent jar fails to load property file. 如何设置可执行的Spring Boot jarfile的JVM属性? - How do I set the JVM properties of an executable Spring Boot jarfile? 无法从appContext.xml在springboot中创建MongoClient Bean:凭证类型转换 - Unable to create MongoClient bean in springboot from appContext.xml: credential type conversion 如何从运行时设置Java jvm属性 - How do I set java jvm properties from run time 如何为JVM设置自定义系统变量以访问属性文件? - how to set custom system variable for JVM to access properties file? 如何从Spring中的JVM选项访问属性值,并在applicationContext.xml中设置一些默认值? - How to access property value from JVM options in Spring with some default values set in applicationContext.xml? 从命令行设置弹簧属性文件位置 - Set spring properties file location from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM