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 configure Spring boot read externalization

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

Share

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

This article mainly explains "how to configure Spring boot read externalization". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to configure Spring boot read externalization.

1. Properties / YAML

We usually write the relevant configuration information in the Properties / YAML file, and then obtain the data in some way.

For example, in the following figure, I defined three parameters:

1.1 Environment

First of all, we can load external configuration information through Environment (all externalized configurations in Spring Boot are loaded into Environment).

First, we inject Environment directly through Autowired:

@ AutowiredEnvironment environment

Then get the data information directly.

@ RestControllerpublic class ConfigController {@ Autowired Environment environment; @ GetMapping ("/ env") public String env () {return environment.getProperty ("name") + "\ n";} 1.2 Value Notes

The second way is our more commonly used annotation method, which directly writes the corresponding KEY through the annotation to obtain the corresponding value. If there is no key value, an error will be reported at startup.

@ Value ("${name}") private String name;2. Customize the Properties file

Sometimes our externalization configuration may not be written in application.properties, but in some custom Properties, so how do we get the data through value annotations?

When talking about environment above, we know that all externalized configurations in Spring Boot will be loaded into Environment, and we can modify it before Spring boot builds environment.

We can modify the environment by implementing the EnvironmentPostProcessor interface and overriding the postProcessEnvironment method.

The overall process is as follows:

Implement interface, rewrite method

Define the Properties, and we need to get the name of the data file.

Parse files to add data to environment

Public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {private final Properties properties = new Properties (); private String propertiesFile = "custom.properties"; @ Override public void postProcessEnvironment (ConfigurableEnvironment environment, SpringApplication application) {Resource resource = new ClassPathResource (propertiesFile); environment.getPropertySources (). AddLast (loadProperties (resource));} private PropertySource loadProperties (Resource resource) {if (! resource.exists ()) {throw new RuntimeException ("file not exist") } try {/ / custom.properties properties.load (resource.getInputStream ()); return new PropertiesPropertySource (resource.getFilename (), properties);} catch (IOException e) {throw new RuntimeException (e);}

After the code is written at this time, can we get the data through value annotations? Let's run it. (remember to empty the data in application.properties)

At this point, we will find that the run failed and did not find the corresponding key. Why is that? This is because the above is our expansion of Spring boot functions, when we do this kind of function expansion, we need to use our SPI mechanism and define our extension implementation according to the expansion rules. Only in this way can our expansion point take effect.

The implementation of the SPI mechanism here is different from that of JAVA. Before doing SPI, the full class name file is created, and then the corresponding implementation class is written. Here, the file name needs to be defined as spring.factories.

Org.springframework.boot.env.EnvironmentPostProcessor=\ com.example.springbootzookeeper.CustomEnvironmentPostProcessor

When we re-run the project at this time, we can find that it is running successfully.

3. Other command parameters

There are many ways to externalize the configuration in Spring boot, such as environment variables, system variables, command line parameters, and so on. We demonstrate a type of externalization configuration below: we can externalize the configuration in this-D form. The key after the value comment corresponds to the key after-D.

@ Value ("${test}") private String command; here, I believe you have a better understanding of "Spring boot read externalization how to configure", might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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