Spring and SpringBoot development Flowchart
- Spring application development process
- Spring Boot application development process
- Spring Boot core features :
- Very low cost of learning
- Spring projects can be run independently.
- "Habit over configuration", which greatly improves the efficiency of the development.
- Minimalist dependent components, automatic discovery and automatic assembly.
- Provides run-time application monitoring
- Cloud computing and distributed architecture and natural integration
- Spring Boot directory structure :
① SpringBoot project created by Maven
- Configuration POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oym</groupId>
<artifactId>my_springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.7.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
- Then in turn create the appropriate package file
② Focus: created by Spring Initializr IDEA provided
Select Web -> Spring Web and configuration files generated, and the above configuration Maven exactly the same (and comes with JUnit dependent)
Spring Boot inlet Class
- The inlet end of the named class typically * Application
- Increasing the inlet annotation class @SpringBootApplication
- Use SpringApplication.run () method to start applications
Spring Boot startup process
Spring Boot in common configuration (change in configuration application.properties)
- application.properties sample configuration is as follows:
#更改端口号
server.port=80
#更改访问页面应用上下文
server.servlet.context-path=/myspringboot
#配置日志打印位置
logging.file.path=o:/my_springboot02.log
#debug(调试日志) - > info(默认) -> warn(警告信息) - > error(异常类) - > fatal(灾难性后果) (等级依次递增)
logging.level.root=info
debug=false
# debug为 true时,会自动调试,并打印调试信息
#配置数据库相关信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=666666
!!! problems: the direct use of application.properties configure a variety of information will cause the code error-prone and difficult to maintain
Solutions are as follows:
Spring Boot supports two profiles
- Properties file: application.properties
- YAML formality: application.yml
YAML syntax
YAML is a simple non-markup language. YAML data-centric, use a blank, indentation, branches organize data so
obtained indicating more concise and easy to read.
- YAML format syntax
- Standard format: key :( space) value
- Use spaces behalf of hierarchy, with ":" End
- application.yml sample configuration is as follows:
debug: true
#logging.level.root
#logging.file
logging:
level:
root: info
file:
path: o:/my_springboot.log
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
data-username: root
data-password: 666666
Spring Boot Custom configuration items
- Spring Boot allows us to customize the application configuration item that allows dynamically loaded at runtime, which provides a good maintainability of the program.
- In the actual project development, we usually custom project information in a configuration file. Specify configuration information can be obtained by @Value.
Spring Boot Environment Profile
- Spring Boot can provide different Profile files for different environments.
- Profile default file naming format application- {env} .yml
- Use spring.profiles.active option to specify a different profile.
For example :
Select the profile files in application.yml
Packaging and run
- Maven package using the command generating Jar package can run independently.
- Start Spring Boot application using the java -jar xx.jar command.
- Jar package can automatically load the application configuration files in the same directory.
- To be continued. . .