Spring 4 - @PropertySource annotation example

Posted on April 22, 2017


Technologies used:   JDK 1.8.0_121 | Spring 4.3.7.RELEASE | MySQL 5.7.12 | PostgreSQL 9.6.1 | Maven 3.3.9 | Eclipse Neon.3

The @PropertySource annotation is used to add any source of key-value pairs to spring’s Environment.   The Environment object represents the spring environment in which the current application is running.

In this post, we will learn how the @PropertySource annotation is used to read the properties files using the @Value annotation and Environment object.

@PropertySource and Environment example

A sample properties file for MySQL configuration.

mysql.properties

# MySQL properties
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/BORAJI
db.username=root
db.password=admin

The following is a @Configuration class to load the above properties in Spring Environment using the @PropertySource annotation.

Use Environment# getProperty() method to access the property value.

MySQLConfig.java

package com.boraji.tutorial.spring;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:mysql.properties")
public class MySQLConfig {

   @Autowired
   private Environment env;

   @Bean
   public DataSource getMySQLDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName(env.getProperty("db.driver"));
      dataSource.setUrl(env.getProperty("db.url"));
      dataSource.setUsername(env.getProperty("db.username"));
      dataSource.setPassword(env.getProperty("db.password"));
      return dataSource;
   }

}

@PropertySource and @Value example

A sample properties file for PostgreSQL configuration.

postgresql.properties

# PostgreSQL properties
jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost:5432/BORAJI
jdbc.username=postgres
jdbc.password=admin

Reading the above properties using the @Value annotation in a @Configuration class.

PostgreSQLConfig.java

package com.boraji.tutorial.spring;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:postgresql.properties")
public class PostgreSQLConfig {

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

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

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

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

   @Bean
   public DataSource getPostgreSQLDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName(jdbcDriver);
      dataSource.setUrl(jdbcUrl);
      dataSource.setUsername(username);
      dataSource.setPassword(password);
      return dataSource;
   }

}

Note – The @Value annotation is used on field, method and method/constructor parameter level to specify a default value.

 

@PropertySource and @PropertySources example

The following @Configuration load the multiple properties files using the @PropertySources and @PropertySource annotation.

DBConfig.java

package com.boraji.tutorial.spring;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;

@Configuration
@PropertySources(value = { @PropertySource("classpath:mysql.properties"),
      @PropertySource("classpath:postgresql.properties") })
public class DBConfig {

   @Autowired
   private Environment env;

   @Bean("mysql")
   public DataSource getMySQLDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName(env.getProperty("db.driver"));
      dataSource.setUrl(env.getProperty("db.url"));
      dataSource.setUsername(env.getProperty("db.username"));
      dataSource.setPassword(env.getProperty("db.password"));
      return dataSource;
   }

   @Bean("postgresql")
   public DataSource getPostgreSQLDataSource() {
      BasicDataSource dataSource = new BasicDataSource();
      dataSource.setDriverClassName(env.getProperty("jdbc.driver"));
      dataSource.setUrl(env.getProperty("jdbc.url"));
      dataSource.setUsername(env.getProperty("jdbc.username"));
      dataSource.setPassword(env.getProperty("jdbc.password"));
      return dataSource;
   }
}

Using placeholder within @PropertySource example

If any ${…} placeholder present in a @PropertySource resource path then Spring will resolve these placeholders using the already registered property source in the Environment.

The following example demonstrates the use of placeholder in @PropertySource resource path.

db.properties

db1.name=mysql
db2.name=postgresql

DBConfig.java


@Configuration
@PropertySources(value = { @PropertySource("classpath:db.properties"),
      @PropertySource("classpath:${db1.name}.properties"),
      @PropertySource("classpath:${db2.name}.properties") })
public class DBConfig {

   @Autowired
   private Environment env;

   @Bean("mysql")
   public DataSource getMySQLDataSource() {
      //...
   }

   @Bean("postgresql")
   public DataSource getPostgreSQLDataSource() {
      //...
   }
}

The above example contains two placeholders - ${db1.name} and ${db2.name}. Spring will load db.properties in environment before resolving the ${db1.name}.properties and ${db2.name}.properties to mysql.properties and postgresql.properties respectively.

 

Download Sources