Spring Boot - Beginner (Overview)

Spring and SpringBoot development Flowchart

  • Spring application development process
    Here Insert Picture Description
  • Spring Boot application development process
    Here Insert Picture Description

  • Spring Boot core features :
  1. Very low cost of learning
  2. Spring projects can be run independently.
  3. "Habit over configuration", which greatly improves the efficiency of the development.
  4. Minimalist dependent components, automatic discovery and automatic assembly.
  5. Provides run-time application monitoring
  6. Cloud computing and distributed architecture and natural integration

  • Spring Boot directory structure :
    Here Insert Picture Description

① 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
    Here Insert Picture Description

② 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

Here Insert Picture Description

Spring Boot in common configuration (change in configuration application.properties)

Here Insert Picture Description

  • 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
  1. Standard format: key :( space) value
  2. 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 :
    Here Insert Picture Description
    Select the profile files in application.yml
    Here Insert Picture Description

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. . .
Published 58 original articles · won praise 7 · views 9240

Guess you like

Origin blog.csdn.net/Mr_OO/article/details/102960976