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 use the @ ConfgurationProperties annotation in SpringBoot

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

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use @ ConfgurationProperties 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!

Source code @ Target ({ElementType.TYPE, ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface ConfigurationProperties {@ AliasFor ("prefix") String value () default ""; @ AliasFor ("value") String prefix () default ""; boolean ignoreInvalidFields () default false; boolean ignoreUnknownFields () default true;} use

Pass the prefix name in the configuration file to the comment, if the configuration file is as follows:

MyConfigs: config1: field1: f1 field2: f2 field3: f3

Then the configuration class in the code should write like this:

@ Component@ConfigurationProperties ("myConfigs.config1") public class MyConfig1 {String field1; String field2; String field3;}

As shown above, the three properties field1, field2, and field3 are bound to the object.

Notice that we use @ Component. In fact, we use configuration classes to inject them into other classes, so we tend to register them as Bean.

IgnoreInvalidFields defaults to false, and the properties of illegal attributes will throw an exception by default

IgnoreUnknownFields defaults to true, and unrecognized attributes will be ignored (so mistyped names will be ignored)

ConfigurationProperties (prefix= "config.prefix", ignoreInvalidFields=true, ignoreUnknownFields=false) public class MyConfig {/ / fields}

The binding rules of Spring Boot are quite loose. MyField, my-field, my_field and so on can all recognize and bind to myField.

You can set the default value for the field so that the default value is used when it is not passed in in the configuration.

@ ConfigurationProperties ("your.prefix") public class YourConfig {private String field = "Default" / / setter}

The fields of the class must have a setter method with public access.

In many cases, the setter method of public is necessary. If you use IDEA, it is recommended that Alt+Insert (Windows, Mac use Alt+n). Of course, you can also use Lombok if you want to.

That's all for the @ ConfgurationProperties annotations 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