Spring @PropertySource

Published on August 3, 2022


Spring @PropertySource

Spring @PropertySource annotation is used to provide properties file to Spring Environment. 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.

Spring PropertySource Example

Let’s quickly go through a simple spring application where we will read Database configuration details from the property file and create the database connection. We will print some metadata information of the database to console. Create a simple maven project and add Spring and MySQL dependencies. You can use any other database too for the example, just change the configurations accordingly.


Below image shows our project final structure, we will go through all the important components one by one. Spring PropertySource Example Here is our class to create the Database Connection.

package com.journaldev.spring;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {

	private String driverClass;
	private String dbURL;
	private String userName;
	private char[] password;
	private Connection con;

	public DBConnection(String driverClass, String dbURL, String userName, char[] password) {
		this.driverClass = driverClass;
		this.dbURL = dbURL;
		this.userName = userName;
		this.password = password;

	public Connection getConnection() {
		if (this.con != null)
			return con;

		Connection con = null;
		try {
			System.out.println("Creating DB Connection");
			con = DriverManager.getConnection(dbURL, userName, String.valueOf(password));
			System.out.println("Successfully Created DB Connection");
		} catch (ClassNotFoundException | SQLException e) {
		this.con = con;
		return con;

	public void close() {
		System.out.println("DBConnection close called");
		if (this.con != null) {
			try {
			} catch (SQLException e) {

NOTE: If you are creating a real-world application, you can use Spring ORM. This way spring will take care of database connection management and you can focus on writing business logic. Now let’s create the Spring Configuration class where we will use PropertySource annotation.

package com.journaldev.spring;

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;

public class DBConfiguration {

    Environment env;
    public DBConnection getDBConnection() {
		System.out.println("Getting DBConnection Bean for App: "+env.getProperty("APP_NAME"));
		DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"), env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"), env.getProperty("DB_PASSWORD").toCharArray());
        return dbConnection;

Notice that I am loading multiple properties files to the Spring environment. Let’s look at these property files content. db.properties

#MYSQL Database Configurations


APP_NAME=PropertySource Example

Let’s create the main class and get database details.

package com.journaldev.spring;

import java.sql.Connection;
import java.sql.SQLException;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringMainClass {

	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

		DBConnection dbConnection = context.getBean(DBConnection.class);

		Connection con = dbConnection.getConnection();


		// close the spring context


Just run the above class as Java application, it will produce following output.

Getting DBConnection Bean for App: PropertySource Example
Creating DB Connection
Successfully Created DB Connection
DBConnection close called

For better readability, I have removed the debug messages produced by spring logging to console.

Spring @PropertySource Multiple Files - @PropertySources

There is another way to load multiple property files for a configuration class.

public class DBConfiguration {

From Java 8 onwards, PropertySource annotation became repeatable. For earlier java versions, @PropertySources was the way to provide multiple property files to the configuration class.

Spring PropertySource Override Values

We can load multiple property files to spring environment. If there are same keys present in multiple files, then the last property file loaded will override the earlier values. So if you are getting unwanted values for your property, check if the same key is present in any other property file and what is the order of loading these property files.

Spring PropertySource External File

Sometimes our configuration files are present on specific location and they are not part of the project classpath. We can configure PropertySource to load property files from file system too.


Spring PropertySource Environment Variable

Notice that the above configuration to read property file from the external location will work for my local system but not for someone else or on the server. We can also read system variables in PropertySource, so below configuration will work for everyone.


Spring PropertySource ignore FileNotFoundException

If the property file is not found, then we will get FileNotFoundException. Sometimes we don’t want to throw exception because our application can work with default values too. We can use PropertySource ignoreResourceNotFound to true to tell Spring framework to don’t throw exception if file is not found.

@PropertySource(value = "classpath:root.properties", ignoreResourceNotFound=true)

That’s all for Spring PropertySource Example. You can checkout source code and maven project from our GitHub Repository.

Spring PropertySource Project

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar


While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
DigitalOcean Employee
DigitalOcean Employee badge
March 19, 2021

Hi Pankaj, very pretty explanation thank you very much this post made my day easy.

- Ramakrishna

    DigitalOcean Employee
    DigitalOcean Employee badge
    July 17, 2019

    Very Good Collections, Excellent Job

    - Sivakumar Dhamodaran

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Featured on Community

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel