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 does springboot get the value of properties attribute

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

Share

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

This article mainly introduces how to obtain the properties attribute value of springboot, which is very detailed and has certain reference value. Friends who are interested must read it!

Summary of the way to get the value of properties attribute

Spring boot in multiple environments, we need to get different values according to different conditions, and we will configure them in different files.

So how do we get the property values of the configuration! Here are several uses.

1. In addition to the default configuration, attributes are added in multiple environments of application.properties

We will activate different ways to select the following different files to publish in application.properties.

Set activation parameters: dev, test, prod

Spring.profiles.active=produrl.lm=editMessageurl.orgCode=100120171116031838url.ybd= http://www.test.com/sales/url.PostUrl=/LmCpa/apply/applyInfo

You can get the attribute and define the configuration class:

@ ConfigurationProperties (prefix = "url") public class ManyEnvProperties {private String lm; private String orgCode; private String ybd; private String postUrl; / / provincial column getter setter method} 2. Use the value form @ Component public class ManyEnvProperties {@ Value ("${url.lm}") private String lmPage; @ Value ("${url.ybd}") private String sendYbdUrl; @ Value ("${url.orgCode}") private String orgCode; @ Value ("${url.PostUrl}") private String PostUrl; / / provincial getter setter method} 3. You can also use Environment in springboot to get the value directly.

Show injection, followed by getting values where needed

@ Autowired private Environment env; logger.info ("=" + env.getProperty ("url.lm"); 4. If you create a new properties file @ Component @ ConfigurationProperties (prefix = "url") @ PropertySource ("classpath:/platform.properties") public class PropertiesEnv {private String lm; private String orgCode; private String ybd; private String postUrl; / / provincial column getter setter method} to get multiple custom attribute values

Using @ Value to inject each custom configuration is troublesome when there are too many property values in the custom configuration. A type-safe configuration method is provided through springboot, which associates the attributes in properties with the attributes of a bean through @ ConfigurationProperties, thus realizing type-safe configuration.

For example, customize the property note.author=yzhnote.name=china in application

Can be passed through

@ ConfigurationProperties (prefix= "note")

It should be noted that the prefix of the custom attribute value is note in order to get the corresponding attribute value. The attribute value name should correspond to the name in the configuration file

At the same time, through this method, you need to generate the get/set method of the attribute value, otherwise you can't get the corresponding attribute value.

This is all the content of the article "how to get the properties attribute value from springboot". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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