Inject Properties from Properties File using @ConfigurationProperties Annotation
spring
75
annotation
8
configuration
1
Male avatar

loveprogramming viết ngày 03/05/2021

https://grokonez.com/spring-framework/inject-properties-from-properties-file-using-configurationproperties-annotation

Inject Properties from Properties File using @ConfigurationProperties Annotation

This tutorial will show you the better way to handle properties using @ConfigurationProperties Annotation.

In previous article, we have learned about using @Value annotation to inject properties from properties file. This approach has disadvantage that if we wanna change any property, we have to update all files which referenced it.

I. Technology

  • Java 1.8
  • Maven 3.3.9
  • Spring Tool Suite – Version 3.8.1.RELEASE (It's OK if you use Eclipse)

    II. Overview

    1. Project Structure

    configproperties-structure1

  • In the project, we use a separate configuration properties classes annotated with @ConfigurationProperties, so property values can be bound to structured objects conveniently.
    With this approach, when we wanna find or change the handling properties object, we just notice a separate component class. In this example, it is class AppicationProperties.

  • For configuration class of the whole application (AppConfig.java), we should enable class above by using @EnableConfigurationProperties annotation.

  • To get environment properties value, we create a Service and inject AppicationProperties object and use getter methods easily.

    2. Step to do

  • Create Maven project

  • Add Dependencies & Plugins

  • Add Properties File

  • Create Properties Class

  • Create Configuration Class

  • Create a Service

  • Create MainApplication Class

  • Run Application & Enjoy Result

    III. Practice

    1. Create Maven project

  • Open Spring Tool Suite, on Menu, choose File -> New -> Maven Project.

  • Check Create a simple project, choose Workspace Location and click Next.

  • Fill all fields in Artifact group box, then click Finish.

    2. Add Dependencies & Plugins

    Open pom.xml, add:

More at:
https://grokonez.com/spring-framework/inject-properties-from-properties-file-using-configurationproperties-annotation

Inject Properties from Properties File using @ConfigurationProperties Annotation

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

451 bài viết.
79 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 6 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 5 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 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/springframework/injectpropertiesusingvalueannotationsvsenvironmentvsconfigurationproperties Inject Properties using @Value An...
loveprogramming viết 16 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

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


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 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á!