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
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.
mvn spring-boot:run
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