'@ConfigurationProperties vs @PropertySource vs @Value

I am new to Spring/Spring Boot. I want to use the key-value pair data of application.properties / application.yml in Java file. I know that we can use @Value in any POJO class to set a default value of a field from application.properties or application.yml file.

Q1) But then why do we need the other two? @ConfigurationProperties and @PropertySource.

Q2) @ConfigurationProperties and @PropertySource, both can be used to load external data mentioned in application.properties or application.yml file? Or any restrictions?

PS: I have tried to search on internet but didn't get a clear answer.



Solution 1:[1]

@Value("${spring.application.name}") @Value will throw exception if there no matching key in application.properties/yml file. It strictly injects property value.

For example: @Value("${spring.application.namee}") throws below exception, as namee field doesn't exists in properties file.

application.properties file
----------------------
spring:
  application:
    name: myapplicationname


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testValue': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.namee' in value "${spring.application.namee}"

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.namea' in value "${spring.application.namee}"

@ConfigurationProperties(prefix = "myserver.allvalues") injects POJO properties, it's not strict, it ignores the property if there is no key in properties file.

For example:

@ConfigurationProperties(prefix = "myserver.allvalues")
public class TestConfigurationProperties {
    private String value;
    private String valuenotexists; // This field doesn't exists in properties file
                                   // it doesn't throw error. It gets default value as null
}

application.properties file
----------------------
myserver:
  allvalues:
    value:  sampleValue

Solution 2:[2]

@ConfigurationProperties is used to map properties with POJO beans. Then you could use bean to access the properties values in your application.

@PropertySource is to reference a properties file and load it.

@Value is to inject a particular property value by it's key.

Solution 3:[3]

Base on my research & understanding::

  • @ConfigurationProperties

    loads properties from application.properties

    you specify the field names to correspond to the names of the properties in application.properties

    -- @ConfigurationProperties does not work with @Value

  • @PropertySource

    loads properties from a file you specify

    can use with @Value or @Autowired Environment env;

  • @Value

    it is used with application.properties

    application.properties is loaded by default (you dont need to specify in @PropertySource)


Reference

https://mkyong.com/spring-boot/spring-boot-configurationproperties-example/

https://mkyong.com/spring/spring-propertysources-example/

-

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/boot-features-external-config.html

-

@ConfigurationProperties annotation. When placed on any Spring bean, it specifies that the properties of that bean can be injected from properties in the Spring environment.

< Spring In Action >

-

You can bundle the configuration file in your application jar or put the file in the filesystem of the runtime environment and load it on Spring Boot startup.

Spring Boot loads the application.properties file automatically from the project classpath.

http://dolszewski.com/spring/spring-boot-application-properties-file/

-

4.1. application.properties: the Default Property File

Boot applies its typical convention over configuration approach to property files. This means that we can simply put an application.properties file in our src/main/resources directory, and it will be auto-detected. We can then inject any loaded properties from it as normal.

So, by using this default file, we don’t have to explicitly register a PropertySource or even provide a path to a property file.

https://www.baeldung.com/properties-with-spring

-

@ConfigurationProperties indicates to spring that it should bind the java fields based on their name to some matching properties.

spring requires that the class that has this annotation must be a spring bean

Spring Inject Values with @ConfigurationProperties and @Value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Molay
Solution 2 Prateek Mehta
Solution 3