Configuration file application. In spring boot Properties usage

1、 Call of configuration document configuration item @ h_ 301_ 4@

After startup, enter @ h directly in the browser_ 301_ 4@http : / / localhost: 18080 / user / test, print the configuration content in the configuration file directly@ H_ 301_ 4@

2、 Bind object bean call @ h_ 301_ 4@

Sometimes there are too many attributes and it is too tired to bind to attribute fields one by one. The official advocates binding an object bean. Here we build a configbean Java class, the annotation @ configurationproperties (prefix = "com") needs to be used at the top to indicate which one to use

After configuration, you also need to add @ enableconfigurationproperties to the spring boot entry class and indicate which bean to load. If you don't write configbean Class, add it to the bean class

Finally, you can use configbean in the controller, as follows:

3、 Reference between parameters @ h_ 301_ 4@

In application The parameters in properties can also be directly referenced, just like the following settings:

In this way, we can just use the property psrinfo

4、 Use custom new profile @ h_ 301_ 4@

Let's create a new bean class as follows:

The main thing is to add an annotation: @ propertysource ("classpath: test. Properties")

5、 Profile priority @ h_ 301_ 4@

application. Properties and application YML files can be placed in the following four locations:

Similarly, the list is sorted by priority, that is, under Src / main / resources / config, application Properties override application. Under Src / main / resources Properties, as shown in the figure:

In addition, if you have applications at the same priority Properties and application YML, then application The attributes in YML will override application Properties.

PS: let's take a look at springboot reading application Properties file @ h_ 301_ 4@

Springboot reads application Properties file, there are usually three ways

1. @ value, for example:

2. @ configurationproperties, for example:

When used in other classes, you can directly inject the testproperties to access the relevant values

3. Use enviroment, for example:

The env method is inefficient

Note: @ configurationproperties can also be used for other applications Properties file, as long as locations is specified

Summary @ h_ 301_ 4@

The above is the spring boot configuration file application Properties, hope to help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>