In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "java uses springboot-starter to check whether the configuration meets the requirements". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "java uses springboot-starter startup to check whether the configuration meets the requirements"!
Check configuration
There are mainly the following detection items:
1. Check of environmental variables
2. Check of java running variables
3. Check the specified location file
4. Host check
Run and start
If we want to run the program as soon as it starts, we naturally think of springboot's starter project. Yes, we encapsulate this into a starter, so that we can check various conditions as soon as we launch the springboot application.
Make starter
Introduce dependencies:
Dependencies {implementation platform ('org.springframework.boot:spring-boot-dependencies:2.1.6.RELEASE') compile group:' org.springframework.boot', name: 'spring-boot-autoconfigure'}
The project structure is shown in the following figure
Spring.factories:
Org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.platform.tools.starter.spring.boot.ToolAutoConfigurationorg.springframework.context.ApplicationContextInitializer=\ com.platform.tools.starter.spring.boot.ToolApplicationContextInitializer
ToolAutoConfiguration.java
@ Configurationpublic class ToolAutoConfiguration {}
ToolApplicationContextInitializer.java
Public class ToolApplicationContextInitializer implements ApplicationContextInitializer {
@ Override public void initialize (ConfigurableApplicationContext applicationContext) {System.out.println ("write check code here");}}
The running effect is as follows:
In this way, we have the effect of running the test code as soon as the project starts.
Write check code
As mentioned above, there are simple checks such as environment variables and files. This paper takes the detection of apollo as an example to write several typical codes to obtain the configuration. The following code includes judging whether the file exists, judging the environment variables, and judging the system variables.
Public class InitCheck {
/ * * operating system category: 1 is win, 2 is other * / private Integer osType
/ * * map * / private Map envMap = System.getenv () of the environment variable
Public InitCheck () {/ / get operating system type String osName = System.getProperty ("os.name"); this.osType = osName.contains ("windows")? 1VO 2;}
/ * the external calling method, which is called after new finishes InitCheck * * @ return * / public boolean checkAll () {return checkApollo ();}
/ * check whether the configuration of apollo is correct * @ return * / private boolean checkApollo () {/ / apollo mainly checks whether ENV is set, and only checks the environment variables and the file String env = envMap.get ("ENV"); boolean envFlag = env! = null & &! env.isEmpty () String filePath = osType==1? "C:/opt/settings/server.properties": "/ opt/settings/server.properties"; boolean fileFlag = Files.exists (Paths.get (filePath)); boolean result = envFlag | | fileFlag; if (! result) {System.out.printf ("Please configure apollo correctly, set the environment variable ENV=dev or write ENV=dev\ n in file% s, filePath);} return result }
}
The running effect is shown in the following figure:
Other check code will not be sent out because it is difficult to desensitize, but the train of thought is the same, you can also write it and try it.
At this point, I believe you have a deeper understanding of "java uses springboot-starter to start to check whether the configuration meets the requirements". You 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.