SpringBoot
SpringBoot concept
Spring Boot is developed based on spring. Spring Boot is designed to let you run spring applications as fast as possible and reduce your configuration as much as possible.
Its design idea is: Convention is greater than configuration
SpringBoot improves and optimizes Spring in order to solve the problems of multiple dependencies and cumbersome configuration of Spring projects
- Start dependence
Starting dependency is essentially a Maven project object model. In short, it is to package the dependency packages of some functions, and you can introduce one.
- Auto configuration
The automatic configuration of springboot means that springboot will automatically register some configuration class bean s into the ioc container.
The expression of "automatic" is that we only need to introduce the packages of the functions we want to use, and the related configurations are ignored. Spring boot will automatically inject these configuration bean s.
SpringBoot project build
-
Build a Spring Boot project using Spring Initializr
-
Fill in package name
- Select the corresponding version, and select the dependency according to the scenario
- Click next to finish.
Introduction to project structure:
Attachment: solve Chinese garbled code
#Set the response to utf-8 spring.http.encoding.force-response=true
Configuration details
1. application.properties configuration file
We can define the relevant properties of the Spring Boot project in the application.properties file. Of course, these related properties can
It refers to system properties, environment variables, command parameters, etc. it can also be the name and location of a user-defined configuration file
server.port=8081 spring.config.location= spring.config.name=application
2. application.yaml configuration file
YAML file format is a JSON superset file format supported by Spring Boot. Compared with the traditional Properties configuration file,
YAML file takes data as the core, which is a more intuitive and easily recognized data serialization format by computer
- The extension of yaml file can use. yml or. yaml
- The application.yml file uses the format of "key: (space) value" to configure the attributes, and indentation to control the hierarchical relationship.
The configuration format of yml is different for different types of attribute values
- The value is a normal data type
server: port: 8081
- The value is an array and a single column set
person: hobby: - aa - bb - cc
or
person: hobby: aa, bb, cc
or
person: hobby: [aa,bb,cc]
- The value is the Map collection and object
person: map: k1: v1 k2: v2
or
person: map: {k1: v1,k2: v2}
3. Profile attribute value injection
- Inject properties using @ ConfigurationProperties
@Component @ConfigurationProperties(prefix = "person") public class Person { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
Profile:
person: id: 2 name: Li Ming
- Use @ Value
package com.mmc.springbootdemo.bean; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component public class Person { @Value("${person.id}") private Integer id; @Value("${person.name}") private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
4. Custom profile
Previously, our configuration files were written in application.properties or application.yml. How can we read the newly created configuration files?
Use @ PropertySource
@Component @ConfigurationProperties(prefix = "test") @PropertySource("classpath:test.properties") public class MyProperties { private Integer id; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
5. Write configuration class
In the Spring Boot framework, it is recommended to add and configure components to the container by configuring classes
In the Spring Boot framework, a Configuration class is usually defined using the @ Configuration annotation, and Spring Boot will automatically scan and identify it
Configure the class to replace the XML configuration file in the traditional Spring framework.
@Configuration // Define this class as a configuration class public class MyConfig { @Bean // Add the return value object as a component to the Spring container, and the component id defaults to the method name public MyService myService(){ return new MyService(); } }