Inject Properties from Properties File using Spring Environment

We can load properties value from Properties file to Spring Environment just by using @PropertySource annotation. This tutorial shows you the simple way to get them from Environment object.

Related Articles:
Inject Properties from Properties File using @Value annotation
Inject Properties from Properties File using @ConfigurationProperties Annotation
How to use Spring Factory Method to create Spring Bean

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

II. Overview

1. Project Structure

springenvironment-structure
In this example, we have 2 ways to access Properties from Environment:
– get Environment from Application Context wherever we wanna use it.
– use a seperate properties bean object to get Environment in the configuration class.

Both of them call method Environment.getProperty(“property_key”) to get property value for corresponding property_key.

2. Step to do

– Create Maven project
– Add Dependencies & Plugins
– Add Properties File
– Create Properties Class
– Create Configuration Class
– Create MainApplication Class
– Run Application & Enjoy Result

III. Practice

1. Create Maven project

– Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project.
– Check Create a simple project, choose Workspace Location and click Next.
– Fill all fields in Artifact group box, then click Finish.

2. Add Dependencies & Plugins

Open pom.xml, add:

<properties>
	<java-version>1.8</java-version>
	<org.springframework-version>4.3.2.RELEASE</org.springframework-version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${org.springframework-version}</version>
	</dependency>
</dependencies>

<build>
	<pluginManagement>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</pluginManagement>
</build>

3. Add Properties File

Under src/main/resources, add properties file:

myapp.properties


app.connection.url=https://ozenero.com
app.connection.name=Java Sample Approach

4. Create Properties Class

Under package config/properties, create ApplicationProperties.java:


package com.javasampleapproach.environment.config.properties;

public class ApplicationProperties {
	private String connectionUrl;
	private String connectionName;

	public String getConnectionUrl() {
		return connectionUrl;
	}

	public void setConnectionUrl(String connectionUrl) {
		this.connectionUrl = connectionUrl;
	}

	public String getConnectionName() {
		return connectionName;
	}

	public void setConnectionName(String connectionName) {
		this.connectionName = connectionName;
	}
}

5. Create Configuration Class

Under package config, create AppConfig.java:


package com.javasampleapproach.environment.config;

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;

import com.javasampleapproach.environment.config.properties.ApplicationProperties;

@Configuration
@PropertySource("classpath:myapp.properties")
public class AppConfig {
	@Autowired
	Environment environment;

	@Bean
	ApplicationProperties appProperties() {
		ApplicationProperties bean = new ApplicationProperties();
		bean.setConnectionUrl(environment.getProperty("app.connection.url"));
		bean.setConnectionName(environment.getProperty("app.connection.name"));
		return bean;
	}
}

6. Create MainApplication Class

MainApp.java


package com.javasampleapproach.environment;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
//import org.springframework.core.env.Environment;

import com.javasampleapproach.environment.config.AppConfig;
import com.javasampleapproach.environment.config.properties.ApplicationProperties;

public class MainApp {

	public static void main(String[] args) {
		AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

		ApplicationProperties appProperties = context.getBean(ApplicationProperties.class);

//		Environment env = (Environment) context.getEnvironment();
//		System.out.println("- connection Url: " + env.getProperty("app.connection.url"));
//		System.out.println("- connection Name: " + env.getProperty("app.connection.name"));

		System.out.println("Properties Values from Properties File:");
		System.out.println("- connection Url: " + appProperties.getConnectionUrl());
		System.out.println("- connection Name: " + appProperties.getConnectionName());

		context.close();
	}
}

7. Run Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Java Application
– Check results in Console Screen:


Properties Values from Properties File:
- connection Url: https://ozenero.com
- connection Name: Java Sample Approach

IV. Source Code

springenvironment

3 thoughts on “Inject Properties from Properties File using Spring Environment”

  1. So, whenever I need to use the property value On that class I need to create context and from that context, I need to retrieve property value and close the context is it?

    1. Another way is to @Autowired Environment object.

      There are other methods to get property value from property file. You can have a comparison view covering those solutions with this post:
      Inject Properties using @Value Annotations vs Environment vs @ConfigurationProperties

      Useful posts:
      Inject Properties from Properties File using @Value Annotation
      Inject Properties from Properties File using @ConfigurationProperties Annotation

      Best Regards!

  2. I’ll right away clutch your rss feed as I can’t to find your email subscription hyperlink or newsletter service. Do you’ve any? Please permit me know so that I may just subscribe. Thanks.

Leave a Reply

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