Spring @PropertySource and @PropertySources Annotations

In this tutorial, we will learn the usage of @PropertySource and @PropertySources annotations in Spring or Spring boot applications.

YouTube Video

@PropertySource and @PropertySources Annotations Overview

Spring @PropertySource annotation is used to provide properties files to Spring Environment.

Spring @PropertySources annotation is used to provide multiple properties files to Spring Environment.

These annotations are used with @Configuration classes.

Spring @PropertySource annotation is repeatable, which means you can have multiple @PropertySource annotations on a Configuration class.

We can use the @Value annotation and Environment class to read the Property file.

@PropertySource and @PropertySources Annotations Example

Create a mail.properties file

Create a mail.properties file under /resources folder in the Spring boot project and add the following content to it:

gmail.host=gmail.com
gmail.email=ramesh@gmail.com
gmail.password=secret

Using @PropertySource annotation

Next, let's use @PropertySource annotation to provide mail.properties file to Spring Environment:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:mail.properties")
public class SpringConfig {
}
Note that @PropertySource annotation is used with @Configuration class.

Next, in order to test this, let's use the @Value annotation and Environment class to read the mail.properties file content from Spring Environment.

Read Property File Using @Value Annotation

Let's use @Value annotation to read the mail.properties file content from Spring Environment:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class PropertySourceDemo {

    @Value("${gmail.host}")
    private String host;

    @Value("${gmail.email}")
    private String email;

    @Value("${gmail.password}")
    private String password;

    public String getHost() {
        return host;
    }

    public String getEmail() {
        return email;
    }

    public String getPassword() {
        return password;
    }
}

Testing

Let's write the code to test:
import net.javaguides.springannotations.lazy.propertysource.PropertySourceDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAnnotationsApplication {

	public static void main(String[] args) {
		var context = SpringApplication.run(SpringAnnotationsApplication.class, args);
		PropertySourceDemo propertySourceDemo = context.getBean(PropertySourceDemo.class);
		System.out.println(propertySourceDemo.getHost());
		System.out.println(propertySourceDemo.getEmail());
		System.out.println(propertySourceDemo.getPassword());
	}
}

Output:

gmail.com
ramesh@gmail.com
secret

Read Property File Using Environment class

Let's use the Environment class to read the mail.properties file content from Spring Environment:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class PropertySourceDemo {

    @Autowired
    private Environment environment;

    //@Value("${gmail.host}")
    private String host;

    //    @Value("${gmail.email}")
    private String email;

    //  @Value("${gmail.password}")
    private String password;

    public String getHost() {
        return environment.getProperty("gmail.host");
    }

    public String getEmail() {
        return environment.getProperty("gmail.email");
    }

    public String getPassword() {
        return environment.getProperty("gmail.password");
    }
}

Testing

Let's write the code to test:
import net.javaguides.springannotations.lazy.propertysource.PropertySourceDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAnnotationsApplication {

	public static void main(String[] args) {
		var context = SpringApplication.run(SpringAnnotationsApplication.class, args);
		PropertySourceDemo propertySourceDemo = context.getBean(PropertySourceDemo.class);
		System.out.println(propertySourceDemo.getHost());
		System.out.println(propertySourceDemo.getEmail());
		System.out.println(propertySourceDemo.getPassword());
	}
}

Output:

gmail.com
ramesh@gmail.com
secret

Using Multiple @PropertySource Annotations

Spring @PropertySource annotation is repeatable, which means you can have multiple @PropertySource annotations on a Configuration class.

For example:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:mail.properties")
@PropertySource("classpath:messages.properties")
public class SpringConfig {
}

@PropertySources Annotation

Spring @PropertySources annotation is used to provide multiple properties files to Spring Environment.
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySources({
        @PropertySource("classpath:mail.properties"),
        @PropertySource("classpath:messages.properties")
})
public class SpringConfig { }

Conclusion

In this tutorial, we discussed the usage of @PropertySource and @PropertySources annotations in Spring or Spring boot applications.

Related Spring and Spring Boot Annotations

Comments