How to use .properties file in Spring using the Environment object?

3k Views Asked by At

I have the following doubt about how works external property in Spring.

For example consider this bean definition (into a Java configuration class):

@Bean
public DataSource dataSource() {
    DataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.postgresql.Driver");
    ds.setUrl("jdbc:postgresql://localhost/transfer" );
    ds.setUser("transfer-app");
    ds.setPassword("secret45" );
    return ds;
}

Ok, as you can see the connection paramethers are hard coded and this is not good so I can externalize these to a properties file.

Reading the documentation I see that I can use also the Environment object to obtain properties from runtime environment, such as:

  • JVM System Properties
  • Java Properties Files
  • Servlet Context Parameters
  • System Environment Variables
  • JNDI

So it seems to me that I can use this Environment object to obtain information from my properties file without having to worry about the procedures for accessing to it. Is it right?

So, for example if I have the following bean configurations into a Java configuration class:

@Configuration
public class ApplicationConfig {

    @Autowired public Environment env;

    @Bean public DataSource dataSource() {
        DataSource ds = new BasicDataSource();
        ds.setDriverClassName( env.getProperty( "db.driver" ));
        ds.setUrl( env.getProperty( "db.url" ));
        ds.setUser( env.getProperty( "db.user" ));
        ds.setPassword( env.getProperty( "db.password" ));

        return ds;
     }
}

it means that I obtain the Environment object using the @Autowired (Spring search the matching object for me) and then I use this object to access to a properties file named db.properties that I have putted somewhere into my code.

Is it right?

Now my doubts are:

1) Exist a standard location for my .properties file into my project or can I put these in any location?

2) In the previous code snippet I don't specify a specific .properties file. How work the Environment object? It automatically load all the .properties file in my project?

2

There are 2 best solutions below

0
On

You should just add @PropertySource annotation:

@Configuration
@PropertySource({"classpath:/com/your/path/app.properties"})
public class ApplicationConfig {

    @Autowired 
    public Environment env;

    @Bean public DataSource dataSource() {
        DataSource ds = new BasicDataSource();
        ds.setDriverClassName( env.getProperty( "db.driver" ));
        ds.setUrl( env.getProperty( "db.url" ));
        ds.setUser( env.getProperty( "db.user" ));
        ds.setPassword( env.getProperty( "db.password" ));

        return ds;
     }
}

I would recommend to read this Spring JavaDoc http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html

0
On
@PropertySources({
 @PropertySource("file:/absolutepath/app.properties"),//IF you want to load from file system using absolute path 
 @PropertySource("classpath:app.properties"),   //IF you want to load from classpath
 @PropertySource("file:{environment_Variable}/app.properties"),   //IF you want to load using envirment varibale 
})