Inject Properties from Properties File using @ConfigurationProperties Annotation
Male avatar

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

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


  • 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 (, 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:

Inject Properties from Properties File using @ConfigurationProperties Annotation

Bình luận

{{ }}
Bỏ hay Hay
Male avatar
{{ comment_error }}

Hiển thị thử

Chỉnh sửa

Male avatar


451 bài viết.
79 người follow
{{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 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 (Ả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 Inject Properties using @Value An...
loveprogramming viết 16 ngày trước
0 0


{{ 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á!