DEV Community

loizenai
loizenai

Posted on

Inject Properties from Properties File using Spring Environment

https://grokonez.com/spring-framework/inject-properties-from-properties-file-using-spring-environment

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:

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:

More at:

https://grokonez.com/spring-framework/inject-properties-from-properties-file-using-spring-environment

Inject Properties from Properties File using Spring Environment

Top comments (0)