Spring @PropertySource Annotation Example

In this section we will learn about @PropertySource Annotation.

The @PropertySource is a convenient annotation for including PropertySource to Spring's Environment and allowing to inject properties via @Value into class attributes. 

This annotation is used with @Configuration classes. Spring @PropertySource annotation is repeatable, means you can have multiple @PropertySource on a Configuration class. This feature is available if you are using Java 8 or higher version. For earlier java versions, @PropertySources was the way to provide multiple property files to the configuration class.

The following example creates a Spring Boot web application which uses @PropertySource annotation.

Project Directory



db.properties

DB_DRIVER_CLASS=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/Demo
DB_USERNAME=knf
DB_PASSWORD=root


message.properties

hello.message=Hello World


knowledgefactory.properties

knf.message=Hi, Greetings from knowledgefactory.net.
language.list=Java, Kotlin, Go, C
user.details={name: 'Sibin', email: 'sibin@gmail.in'}


Run the application - Application.java

Below I am loading multiple properties files to the Spring environment. 

package com.knf.dev.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@PropertySource(value = "knowledgefactory.properties")
@PropertySource(value = "message.properties")
@PropertySource(value = "db.properties")
public class Application implements CommandLineRunner {

@Value("${knf.message}")
private String knfMessage;

@Value("${hello.message}")
private String hello;
@Value("${language.list}")
private List<String> languageList;

@Value("#{${user.details}}")
private Map<String, String> userDetails;

@Autowired
Environment env;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {

System.out.println("Knowledgefactory message= " + knfMessage);
System.out.println("List of all users== " + languageList);
System.out.println("User details= " + userDetails);
System.out.println("Hello world message= " + hello);
System.out.println("DB DRIVER CLASS="+
env.getProperty("DB_DRIVER_CLASS"));
System.out.println("DB URL="+env.getProperty("DB_URL"));
System.out.println("DB USERNAME="+
env.getProperty("DB_USERNAME"));
System.out.println("DB PASSWORD="+
env.getProperty("DB_PASSWORD"));
}
}

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run

Console Output:
Knowledgefactory message= Hi, Greetings from knowledgefactory.net.
List of all users== [Java, Kotlin, Go, C]
User details= {name=Sibin, email=sibin@gmail.in}
Hello world message= Hello World
DB DRIVER CLASS=com.mysql.jdbc.Driver
DB URL=jdbc:mysql://localhost:3306/Demo
DB USERNAME=knf
DB PASSWORD=root


Download Source Code

More related topics,

Spring Web Annotations


Spring Core Annotations

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete