简体   繁体   中英

Reading From Dynamically Updated Application.properties

I have application properties file which I am dynamically updating using maven build step.

mvn clean -Dusername=user1 -Durl=xxxx -Dpassword=xxxx -DskipTests install

jdbc.url=${url}
jdbc.username=${username}
jdbc.password=${password}

I am reading these properties in the configuration class

@Configuration 
@ImportResource("classpath:/spring-beans.xml")
@PropertySource("classpath:/application.properties")
public class ApplicationConfiguration {

  @Value("${jdbc.url}")
  private String url;

 @Value("${jdbc.username}")
  private String username;

 @Value("${jdbc.password}")
 private String password;

@Bean(name = "c3p0DataSource")
public ComboPooledDataSource dataSource() throws PropertyVetoException, 
IOException {

logger.info("Creating Datasource for {}",System.getenv("SPRING_DATASOURCE_URL"));
// logger.info("Creating Datasource for username {}", 
prop.getProperty("username"));
logger.info("Creating Datasource for {}", System.getenv("username"));
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");

logger.info("User Name :" + username);//returning $username instead of user1

logger.info("password :" + password);

System.out.println("User name : " + username);

dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;  }   }

I am not getting updated values instead I am getting $username, $password as values, can anyone help me what i am missing here ?

my modified properties file look like below

jdbc.url=xxxx
jdbc.username=user1
jdbc.password=xxxx

Rather than using Spring's application property , I will suggest you to use another property file, store it on file system and use org.apache.commons.configuration.PropertiesConfiguration class to load values from this file.

org.apache.commons.configuration.PropertiesConfiguration has capability to reload property file on change.

https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

If you are using maven add below dependency.

<dependency>
    <groupId>commons-configuration</groupId>
    <artifactId>commons-configuration</artifactId>
    <version>1.10</version>
</dependency>

When you say 'dynamically updated' it seems to me you just mean updated at build time and not at runtime. If so then you need to use the maven resources plugin, define the maven variables and use a different syntax in the properties file. This is c overed in the properties and configuration section of the spring boot documentation

你应该跑

mvn clean -Djdbc.username=user1 -Djdbc.url=xxxx -Djdbc.password=xxxx -DskipTests install

I have tried initialising data manually, which works. You can also give it a try.

You can try out the code below:

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Configuration;


@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationConfiguration {

    private Properties properties = new Properties();
    public static String driverClass;
    public static String dataSourceUrl;
    public static String dataSourceUser;
    public static String dataSourcePassword;

    public ApplicationConfiguration() throws IOException {
        properties.load(new InputStreamReader(ApplicationConfiguration.class.getResourceAsStream("/application.properties")));    
        driverClass = properties.getProperty("spring.datasource.driver-class-name");
        dataSourceUrl = properties.getProperty("spring.datasource.url");
        dataSourceUser = properties.getProperty("spring.datasource.username");
        dataSourcePassword = properties.getProperty("spring.datasource.password");
  }

  // Other Code Details

}

Now I can easily use it like: ApplicationConfiguration.driverClass or ApplicationConfiguration.dataSourceUser .

Few other resources are also used by me from application.properties which I am not initialising manually and also not required while building jar . So only I am using @PropertySource("classpath:application.properties") to use other resources without initialising manually.

Try it once, It may help you :)

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