简体   繁体   中英

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. This config.properties contains propertis like 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>

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.

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.

in Spring BOOT i've made this and its worked:

        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}")

I don't know why, but System.setProperties(properties); not worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM