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

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 12 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 10 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 10 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/springframework/springbeanscopeannotationrequestscopesessionscopeapplicationscope Spring Bean Scope Annotation: @RequestScope...
loveprogramming viết 6 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!