Spring provides an easy way to configure/load properties to Environment using @PropertySource annotation. Environment will be available through ApplicationContext using applicationContext.getEnvironment().

1. Simple @ProperySource and Environment usage


1.1 Create env.properties

Create property file under resource folder with following properties

jdbc.url=127.0.0.1:1521
jdbc.userid=scott

1.2 Define @PropertySource

Define @PropertySource to the class annotated with @Configuration. This will enable spring to load property which will be accessed using Environment.

@Configuration
@PropertySource("classpath:env.properties")
public class AppConfig {
	
}
Note:If you wanted to use ${..} operator to resolve properties Ex: @PropertySource(“classpath:${path}/env.properties”), please define PropertySourcesPlaceholderConfigurer bean in configuration class.

1.3 Access property using Environment.

package com.codesimplify;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.Environment;
import com.codesimplify.config.AppConfig;

public class SpringPropertyLoadApp {

	public static void main(String[] args) {		
			    
		  ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		  Environment env = (Environment) context.getEnvironment();
		  
		  System.out.println("DB URL:"+ env.getProperty("jdbc.url") );
		  System.out.println("DB User:"+ env.getProperty("jdbc.userid") );			
	}
}

Output:

DB URL:127.0.0.1:1521
DB User:scott

2. Use Environment in Configuration class


2.1 Create env.properties


Create property file under resource folder with following properties

jdbc.url=127.0.0.1:1521
jdbc.userid=scott

2.2 Define a Spring bean class to hold Database Properties


package com.codesimplify.config.beans;

public class DatabaseConfigBean {
	
	private String dbUrl;
	
	private String dbUserId;
	
	public String getDbUrl() {
		return dbUrl;
	}

	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}

	public String getDbUserId() {
		return dbUserId;
	}

	public void setDbUserId(String dbUserId) {
		this.dbUserId = dbUserId;
	}

}

2.3 Define @PropertySource and Autowire Environment

  • Define @PropertySource to the class annotated with @Configuration. This will enable spring to load property which will be accessed using Environment.
  • Autowire Environment variable to inject properties
  • Wire properties to Spring bean in configuration class.
@Configuration
@PropertySource("classpath:env.properties")
public class AppConfig {
	@Autowired
	Environment env;

	@Bean
	DatabaseConfigBean dbConfigBean() {
		DatabaseConfigBean bean = new DatabaseConfigBean();
		bean.setDbUrl(env.getProperty("jdbc.url"));
		bean.setDbUserId(env.getProperty("jdbc.userid"));
		return bean;
	}
}

2.4 Run the Application.

public class SpringEnvironmentUsageApp {

	public static void main(String[] args) {		
			    
		  ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		  DatabaseConfigBean dbConfigBean = (DatabaseConfigBean) context.getBean("dbConfigBean");
			    
		  System.out.println("DB URL:"+ dbConfigBean.getDbUrl() );
		  System.out.println("DB UserId:"+ dbConfigBean.getDbUserId() );

		 
	}
}

Output:

DB URL:127.0.0.1:1521
DB User:scott

References


Spring @PropertySource

Spring javaconfig @PropertySource and Environment Example
Tagged on:         

3 thoughts on “Spring javaconfig @PropertySource and Environment Example

Leave a Reply

Your email address will not be published. Required fields are marked *