Spring @PropertySource annotation tutorial
last modified October 18, 2023
Spring @PropertySource annotation tutorial shows how to use @PropertySource annotation to include properties into the Environment and inject properties with @Value.
Spring is a popular Java application framework for creating enterprise applications.
Spring @PropertySource
@PropertySource
is a convenient annotation for including PropertySource
to Spring's Environment and allowing to inject properties via @Value
into class
attributes. (PropertySource
is an object representing a set of property pairs
from a particular source.)
@PropertySource
is used together with @Configuration
.
Spring @PropertySource example
The application uses Spring's @PropertySource
to include properties from
the application.properties
file into the Environment and to inject them
into class attributes.
pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───config │ │ AppConfig.java │ └───resources │ application.properties │ logback.xml └───test └───java
This is the project structure.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>propertysource</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <spring-version>5.3.23</spring-version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <configuration> <mainClass>com.zetcode.Application</mainClass> </configuration> </plugin> </plugins> </build> </project>
In the pom.xml
file, we have basic Spring dependencies spring-core
,
spring-context
, and logging logback-classic
dependency.
The exec-maven-plugin
is used for executing Spring application from the
Maven on the command line.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <logger name="org.springframework" level="ERROR"/> <logger name="com.zetcode" level="INFO"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration>
The logback.xml
is a configuration file for the Logback logging library.
app.name=My application app.version=1.1
We have two properties in application.properties
file.
package com.zetcode.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource(value = "application.properties", ignoreResourceNotFound = true) public class AppConfig { }
AppConfig
is the application configuration class. The @PropertySource
injects properties from the application.properties
into the Spring's Environment.
package com.zetcode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.env.Environment; @ComponentScan(basePackages = "com.zetcode") public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); @Autowired private Environment env; @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; public static void main(String[] args) { var ctx = new AnnotationConfigApplicationContext(Application.class); var app = ctx.getBean(Application.class); app.run(); ctx.close(); } private void run() { logger.info("From Environment"); logger.info("Application name: {}", env.getProperty("app.name")); logger.info("Application version: {}", env.getProperty("app.version")); logger.info("Using @Value injection"); logger.info("Application name: {}", appName); logger.info("Application version: {}", appVersion); } }
In the Application
, we get the properties using two methods.
@Autowired private Environment env;
We inject the Environment
. We can retrieve the properties with
its getProperty
method.
@Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion;
We inject the properties with @Value
annotation into the attributes.
logger.info("From Environment"); logger.info("Application name: {}", env.getProperty("app.name")); logger.info("Application version: {}", env.getProperty("app.version"));
The first way to retrieve properties is from the Environment
using
the getProperty
method.
logger.info("Using @Value injection"); logger.info("Application name: {}", appName); logger.info("Application version: {}", appVersion);
The second way is to use the injected attributes.
$ mvn -q exec:java 15:00:20.653 INFO com.zetcode.Application - From Environment 15:00:20.668 INFO com.zetcode.Application - Application name: My application 15:00:20.668 INFO com.zetcode.Application - Application version: 1.1 15:00:20.668 INFO com.zetcode.Application - Using @Value injection 15:00:20.668 INFO com.zetcode.Application - Application name: My application 15:00:20.668 INFO com.zetcode.Application - Application version: 1.1
We run the application.
In this article we have shown how to use @PropertySource
annotation to conveniently
work with properties in a Spring application.
Author
List all Spring tutorials.