Refinement Spring Boot 03:Spring Boot Configuration file and configuration management , And read the configuration file in three ways
Content abstract : In this paper, Spring Boot Configuration file and configuration management , And three ways to read configuration files , And code demonstration .
stay Spring Boot Before appearance , One Spring There are generally many configuration files in projects , For example, there are configurations and web Server related information web.xml, Have configuration Spring Its own variety application-xxx.xml, One copy xxx Corresponding Sping A function module of , for example MVC and Data Source, And third party plug-ins Such as log4j Etc . Besides , The application itself may also have multiple configuration files , Just counting the number of these configuration files makes people feel dizzy , Let alone manage and configure them .
Spring Boot The arrival of Technology , It greatly simplifies the management and reading of these configuration files . about Spring. Configuration information , Configuration information of the third-party framework it supports , Apply custom configuration information , All integrated in one application.properties In file , There may have been near 10 A new configuration file , Now only one is needed . Don't be like before , The configuration information is scattered in different configuration files of the application . This obviously greatly simplifies the management of configuration information , Of course, it also reduces the probability of error .
1、Web Server configuration
Spring Boot The default to start web Server is Tomcat, The port is 8080, The context is “/”. For these default settings , We can all be in appication.properties Reconfigure .
We're in appication.properties Add two lines of code to
server.port=9090
server.servlet.context-path=/home
【 chart 1 Set server properties 】
Press shortcut key Shift + F10 Run the program , Enter the original website http://localhost:8080/hello, Server not found . The reason is that we have used the above two lines of code to modify Tomcat Port number and application context . We can see the result by entering the following URL .
http://localhost:9090/home/hello
【 chart 2 Hello, Spring Boot】
2、 use Undertow Replace the default Tomcat The server
Spring Boot Default built-in web Server is Tomcat. in fact , It supports three nested web The server , The other two are Jetty and Undertow, and Undertow Best performance , You can set it to web The server , Replace the other two .
Spring Boot Of spring-boot-starter-web Nested by default Tomcat. When we change to something else web Server time , If replaced with Undertow, Need to put Tomcat To block out . meanwhile , It is also necessary to introduce corresponding web The server corresponds to starter.
Now we're trying to put web The server is switched to Undertow, Modify as follows pom.xml
【 chart 3 To configure Undertow 】
The switch found on the Internet web Most server solutions are as follows , But in IDEA Compile and run the program , It's always
Stubbornly use Tomcat, Instead of enabling Undertow. At this time, there is enough reason to suspect that this is IDEA Caused by cache problems , You need to refresh like this IDEA Of Maven project .
【 chart 4 IDEA Refresh Maven project 】
Refresh Maven After the project , And then use Maven To compile the program , Double click... As shown in the figure below install.
【 chart 5 Maven Install 】
After compiling , Press shortcut key Shift + F10 Run the program , You can see the log output display Undertow It's started successfully .
【 chart 6 Undertow Successful startup 】
3、 Read Spring Boot Configuration of
Can be read in the application application.properties Configuration file for , And get the configuration information , For application use . in fact Spring Boot Provides three ways to read configuration files .
The first 1 One way is through Environment class Of getPropery() Method reading .
The first 2 One way is through @Value Annotation to automatically inject attribute values .
The first 3 One way is to automatically inject a set of attributes into a configuration class .
We first in application.properties Add the following four lines of configuration , Then use three methods to read .
app.message1=Hi, Felix!
app.message2=You are really an IT expert.
app.num1=4
app.num2=5
Now modify HelloWorld Of sayHello Method , Use @Value Annotation method to read app.message1, use Enviornment.getProperty() Read app.message2, use @Autowired hold Environment Inject in , You can call its getProperty() The method . The code is as follows :
【 chart 7 Environment & @Value】
After starting the program , Successfully read the data of the configuration file in two ways , The output in the browser is as follows :
【 chart 8 Felix IT Expert】
The first 3 One way to read the configuration file is to automatically inject a set of attributes into a configuration class , In this case, you need to use annotations @ConfigurationProperties and @Configuration, We read... In this way app.num1 and app.num2 Value , And sum them , Then output the result . Or code .
【 chart 9 Configuration Properties 】
AppConfig Class uses two annotations @ConfigurationProperties and @Configuration, stay @ConfigurationProperties The parameter... Is entered in “app”, It's a property app.num1 and app.num2 The prefix of , As you can see from the following code AppConfig It's just an ordinary POJO file , It's just a note .
Next, let's look at how to use AppConfig This class . Now let's create a new summation controller SumController, Using annotations @Autowired hold AppConfig Automatically inject in , You can use it , Is it convenient 、 It's simple .
【 chart 10 Summation controller 】
Press Shift + F10 Start the program , Input Sum Method corresponding to url http://localhost:9090/home/sum, You can see the result .
【 chart 11 Summation result 】
Okay , The third chapter written by Feixian spacetime Spring Boot To this end , Welcome to forward and follow .
Enjoy coding.