Spring boot core
1. Basic Configuration of Spring Boot
1.1 Start classes and core annotations @SpringBootApplication
SpringBoot applications usually have a program entry class named *Application that uses SpringBoot’s core annotation @SpringBootApplication to mark the startup class of the pseudo Application.
Under this startup class, there is a standard Java Application’s main method. In the main method, “SpringApplication.run(* Application.class,args)” starts the Spring Boot Application.
The demo is as follows:
package com.test.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootDemoApplication.class, args); }}Copy the code
SpringBoot’s core annotation @SpringBootApplication is a composite annotation that mainly combines @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan annotations
-
@ SpringBootConfiguration annotations
Is a Configuration annotation for the Spring Boot application, replacing the @Configuration annotation
-
@ EnableAutoConfiguration annotations
You can have Spring Boot automatically configure the configuration of a project based on the JAR that the current application project depends on.
-
@ ComponentScan annotations
The annotation allows SpringBoot to automatically scan the configuration of the @SpringBootApplication class’s siblings and its children, so the entry class is typically placed under the project package
1.2 Disabling a specific automatic configuration
If a developer does not need a particular automatic configuration of SpringBoot, it can use the @SpringBootApplication exclude parameter to turn off that particular automatic configuration
@SpringBootApplication(exclude={***Configuration.class})
1.3 custom Banner
When you start a Spring Boot application, you can see the default Boot pattern on the console. How do you want to see the Boot information you specify
- Create a new banner. TXT file under SRC /main/resources/ and add any string content
- Open the web page patorjk.com/software/ta… , enter the custom string, then click select & Copy in the web page, and copy to bannner.txt
Close the banner
Add the following configuration in application.properties
spring.main.banner - mode=off
1.4 Spring Boot Global Configuration file
The Spring Boot global configuration file (application.properties or application.yml) can be placed in the SRC /main/resources directory of the Spring Boot project or in the /config directory of the classpath
1.4.1 Setting a port Number
server.port=8888
1.4.2 Setting the Context Path of a Web Application
server.servlet.context - path=/xxx
This time by https://localhost:8080/xxx/hello access to the following request processing method of the controller class
@RequestsMapping("/hello")
public String index(a){
……
}
Copy the code
1.4.3 Configuration Documents
In the global configuration file of Spring Boot, you can configure and modify multiple parameters. For more details, see the official documentation
1.5 Spring Boot
Spring Boot offers a number of out-of-the-box Starters that can automatically correlate dependencies for a project as long as you use the specific Starters
1.6 Reading Application Configuration information
Spring Boot provides three ways to read the contents of a project’s application.properties configuration file. These are the Environment class, the @Value annotation, and
@ ConfigurationProperties annotations
1.6.1 Environment
Environment is a generic class that reads application. Properties, command line input parameters, system properties, operating system Environment variables, and so on in key-value mode.
package com.test.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ReadConfigController {
@Autowired
private Environment env;
@RequestMapping("/testEnv")
public String testEnv(a){
return Test. MSG = test. MSG;+env.getProperty("test.msg"); }}Copy the code
1.6.2 @ Value
Reading the contents of the configuration file using the @value annotation is simple
package com.test.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ValueReadConfigController {
@Value("${test.msg}")
private String msg;
@RequestMapping("/testValue")
public String testvalue(a){
return "Read test. MSG from configuration file with @value"+msg; }}Copy the code
1.6.3 @ ConfigurationProperties
Use @ConfigurationProperties to first map the configuration file to the object, and then use the @AutoWired annotation in the controller method to write the object
Entity class
package com.test.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="test")
public class msgProperties {
private String msg;
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg(a) {
return msg;
}
@Override
public String toString(a){
return "Read the value of test. MSG in the configuration file with @configurationProperties."+msg; }}Copy the code
controller
package com.test.demo.controller;
import com.test.demo.msgProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigurationPropertiesController {
@Autowired
msgProperties msgp;
@RequestMapping("/testConfigurationProperties")
public String test(a){
returnmsgp.toString(); }}Copy the code
1.6.4 @ PropertySource
If we want to read a configuration file other than the global configuration file, we can use PropertySource
package com.test.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@PropertySource({"test.properties","ok.properties"})
public class PropertySourceValueReaderOtherController {
@Value("${test.msg}")
private String testmsg;
@Value("${ok.msg}")
private String okmsg;
@RequestMapping("/testProperty")
public String testProperty(a){
return "Other configuration files test.properties:"+testmsg+"<br>"+"ok.properties:"+okmsg; }}Copy the code