Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement configuration binding with @ ConfigurationProperties annotation in SpringBoot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to implement configuration binding with @ ConfigurationProperties annotations in SpringBoot". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The properties configuration file is as follows:

Human.name=Mr.Yuhuman.age=21human.gender=male

How to bind the configuration in properties to JavaBean, we used to do the following:

Public class PropertiesUtil {public static void getProperties (Person person) throws IOException {Properties properties = new Properties (); properties.load (new FileInputStream ("src/main/resources/demo.properties")); / / get the name of the configuration file key Enumeration enumeration = properties.propertyNames (); while (enumeration.hasMoreElements ()) {String key = (String) enumeration.nextElement (); String value = properties.getProperty (key) System.out.println ("key=" + key+ "- value=" + value); / / encapsulated into JavaBean / / the following content is omitted}

Output result:

Key=human.name-value=Mr.Yu

Key=human.age-value=21

Key=human.gender-value=male

Process finished with exit code 0

You can see that the process is cumbersome, but it will be very simple in SpringBoot.

There are three methods in SpringBoot:

1.@ConfigurationProperties comments + @ Component (or @ Controller or @ Service or @ Repository) comments

Only the components in the container have the powerful capabilities provided by SpringBoot, that is, if we need to use the @ ConfigurationProperties annotation, then we first need to ensure that the pair of JavaBean objects are in the IoC container, so we need to use the Component annotation to add components to the container.

Prefix and value are aliases to each other in @ ConfigurationProperties annotations

In other words, @ ConfigurationProperties (value = "human") is the same as @ ConfigurationProperties (prefix = "human").

Prefix and value mean prefixes.

Code testing: properties configuration file:

Human.name=Mr.Yuhuman.age=21human.gender=male

Person class:

@ Component / only components in the container will have the powerful features provided by SpringBoot @ ConfigurationProperties (value = "human") public class Person {public String name; public int age; public String gender; public Person () {} public Person (String name, int age, String gender) {this.name = name; this.age = age; this.gender = gender } public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getGender () {return gender;} public void setGender (String gender) {this.gender = gender } @ Override public String toString () {return "Person {" + "name='" + name +'\'+ ", age=" + age + ", gender='" + gender +'\'+'}';}}

The test classes are as follows:

/ / @ Controller////@ResponseBody is written to the browser as a string instead of jumping to a page / / @ ResponseBody//@RestController = @ Controller + @ ResponseBody@RestControllerpublic class HelloController {/ / automatic injection attribute @ Autowired Person person; @ RequestMapping ("/ person") public Person getPerson () {return person;}}

Test results:

@ ConfigurationProperties comment + @ EnableConfigurationProperties comment

In this way, the @ EnableConfigurationProperties annotation must be written on the configuration class. @ EnableConfigurationProperties means to enable the property configuration function. Whose property configuration function is enabled? because our above Person class wants to configure and bind, we add the parameter Person.class:@EnableConfigurationProperties (Person.class) after it.

The purpose of @ EnableConfigurationProperties (Person.class) is to register the Person component with the container. The object name is: human-com.ysw.boot.properties.Person, where human is the value of the prefix value in @ ConfigurationProperties (value = "human").

There is still a @ ConfigurationProperties annotation on the Person class, which replaces the @ Component annotation on the Person class with the @ EnableConfigurationProperties (Person.class) annotation on the configuration class

This method is mainly used when referencing a third-party package, for example, there is a Person class in the third-party package that does not use @ Component, and we cannot add @ Component to the class in the third-party package. In this case, we can use the method of annotating the configuration class with @ EnableConfigurationProperties.

@ ConfigurationProperties comment + @ Import comment

Use @ Import to automatically create this type of component in the container. The default component name is the full class name.

The @ Import annotation has the same effect as the @ ConfigurationProperties annotation in 2

It's just that they register with a different name in the container:

The @ ConfigurationProperties annotation registers the name of the object in the container as: human-com.ysw.boot.properties.Person, where human is the value of the prefix value in @ ConfigurationProperties (value = "human").

The @ Import annotation registers the name of the object in the container as: com.ysw.boot.properties.Person

Configuration class:

Person class:

Test class:

Application.properties file:

Server.port=8888human.name=Mr.Yuhuman.age=21human.gender=male222

Test results:

That's all for "how to implement configuration binding with the @ ConfigurationProperties annotation in SpringBoot". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report