Logo

dev-resources.site

for different kinds of informations.

Inject Properties using @Value Annotations vs Environment vs @ConfigurationProperties

Published at
4/28/2021
Categories
springboot
configurationproperties
annotation
Author
loizenai
Author
8 person written this
loizenai
open
Inject Properties using @Value Annotations vs Environment vs @ConfigurationProperties

https://grokonez.com/spring-framework/inject-properties-using-value-annotations-vs-environment-vs-configurationproperties

Inject Properties using @Value Annotations vs Environment vs @ConfigurationProperties

@Value Annotations vs Environment vs @ConfigurationProperties.

Spring provides some ways to inject properties from properties file. We have learned how to use them in 3 articles:

This tutorial helps you have a comparison view covering those solutions.

1. @Value Annotation

We can get property value anywhere by using @Value, we can inject default value, List/Arrays, special types (DateTime, Pattern), value that should be treated as null easily with just one line added. So it is very easy to implement.


@DateTimeFormat(pattern="yy/MM/dd HH:mm")
@Value("${config.time.key}")
private LocalDateTime time;

But to be sure that this is possible, PropertySourcesPlaceholderConfigurer must exist in the application contexts that placeholders resolution is required.

    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
//      return new PropertySourcesPlaceholderConfigurer();
        
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
//      configurer.setLocation(new ClassPathResource("myapp.properties"));
        
        configurer.setIgnoreUnresolvablePlaceholders(true);
        configurer.setNullValue("This is NULL");
        
        return configurer;
    }

This approach also has another disadvantage that it makes each class which uses @Value depends on one or more property name. If we wanna change any property, we have to update all files which referenced it, or if we wanna find where it is used, we must make a text searching.

2. Environment

To work with Environment, we use @Autowired:

More at:

https://grokonez.com/spring-framework/inject-properties-using-value-annotations-vs-environment-vs-configurationproperties

Inject Properties using @Value Annotations vs Environment vs @ConfigurationProperties

annotation Article's
30 articles in total
Favicon
The Power of Bounding Box Annotation in AI Development
Favicon
Usando annotations em Java para fazer um strategy
Favicon
Using a HashMap in a custom Annotation
Favicon
The Logging, The AOP and The Clean Code
Favicon
Delving into Spring Data JPA: Mastering Mutual One-to-Many Dependencies
Favicon
Image Labeling in React
Favicon
Custom annotation to check authorization in Spring
Favicon
Kubernetes Essentials: Labels, Selectors, Environment Variables, and Annotations Demystified
Favicon
Landmark Annotation: Key Points
Favicon
The Role of Medical Image Segmentation in Diagnostics and Treatment Planning
Favicon
Using ChatGPT to migrate from PHP annotations to attributes
Favicon
Java – How to Add Stamps to a PDF Document
Favicon
Secure Your Business Applications with Spring Boot Annotation
Favicon
PDF.js annotation library in pure JavaScript. Create and save PDF annotation, (pdf highlight/signature)- pdf.js
Favicon
SSO (Single Sign-On) for CVAT, the Open Source annotation tool
Favicon
PDF Reviewer (2) - Why is it built?
Favicon
PDF Reviewer (1) - What is it?
Favicon
Hello from pdfreviewer.com
Favicon
Automatic Data Classification on t6 IoT
Favicon
Custom Annotation Scanning with Spring Boot
Favicon
Dynamic Configuration of Java Plugins
Favicon
How to Add Annotations in React Charts of Syncfusion
Favicon
@Resource Annotation in Spring
Favicon
Inject Properties using @Value Annotations vs Environment vs @ConfigurationProperties
Favicon
SpringBoot WebFlux Annotation-based RestAPIs
Favicon
Spring Bean Scope Annotation: @RequestScope | @SessionScope | @ApplicationScope
Favicon
Add Annotations to PDF in Java
Favicon
Converting explicit into implicit configuration in Spring
Favicon
Effective Java! Prefer Lambdas to Anonymous Classes
Favicon
Effective Java! Use Marker Interfaces to Define Types

Featured ones: