What is Spring Boot?

Spring Boot is a collection of libraries that can be used by any project’s build system to simplify the initial setup and development of new Spring applications, simplify configuration, and integrate other third-party technologies in an easier way.

usespring bootWhat are the benefits?

It’s simple, fast and convenient!

What do we usually do when we need to build a Spring Web project?

  1. Configure web.xml and load Spring and Spring MVC
  2. Configure the database connection and spring transactions
  3. Config load config file reading, enable annotations
  4. Configuring log Files

.

Deploy Tomcat debugging after the configuration is complete

How much fun it is to use the Sping Boot

Systematic summary

The role of SpringBoot

  • Simplified configuration file

  • Integrate third party technology in an easier way

    • Redis
    • ElasticSearch
    • MyBatis

Two, the use of SpringBoot routines

  • Add the required scenario starter dependency
  • Configure properties or YML
  • Create the main startup class
  • Enable functionality through annotations
  • Run the main startup class

Third, the HelloWorld

1. Procedure

① Create Maven project

Add a dependency

<! > <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <dependencies> <! Add a scenario launcher needed for Web development --> <dependency> <! GroupId and artifactId GroupId >org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>Copy the code

③ Create the main startup class

(4) Create HelloWorldHandler, now created hanlder will not be scanned automatically, because only the subpackages of the main startup class will be scanned automatically

The next one is correct

SpringBootHelloWorld

@springBootApplication public class SpringBootHelloWorld {public static void main(String[] args) { SpringApplication.run(SpringBootHelloWorld.class, args); }}Copy the code

5. Start

Start the SpringBoot program by running the main method in the main boot class.

⑥ Access the handler method through the web page

2. Principle exploration

① Core mechanics

Note:

Spring will boot default load org. Springframework. Boot. Autoconfigure. JDBC. DataSourceAutoConfiguration class, DataSourceAutoConfiguration classes use the @ Configuration annotations to the spring inject the dataSource bean. Spring creates a dataSource bean without any configuration information about the dataSource. Increase in Application class @ EnableAutoConfiguration (exclude = {DataSourceAutoConfiguration. Class}) to prevent spring boot automatically inject the dataSource Bean, followed by the druid dataSoure configuration, there is no need to add the annotation @ EnableAutoConfiguration (exclude = {DataSourceAutoConfiguration. Class})Copy the code
@springBootConfiguration: The @Configuration annotation is combined to realize the function of the Configuration file. @enableAutoConfiguration: Enable the automatic configuration function or disable an automatic configuration option, such as data source automatic configuration: @ SpringBootApplication (exclude = {DataSourceAutoConfiguration. Class}). @ComponentScan: Spring component scanning.Copy the code

Configuration files in the SpringBoot environment

1. An overview

Two configuration files are commonly used in the SpringBoot environment: properties file and YML file. The two have their own characteristics, and the syntax is very different, but the end result is basically the same.

2. Properties file usage

File name: application.properties

Properties file syntax format:

server.port=8074 spring.datasource.url=jdbc:mysql://localhost:3306/gmall? characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root mybatis.configuration.map-underscore-to-camel-case=true mybatis.mapper-locations=classpath:mapper/*Mapper.xml Spring. Dubbo. Registry. Protocol = zookeeper spring. The dubbo. Registry. Address = 192.168.0.100:2181 spring.dubbo.application.name=gmall-cart-service spring.dubbo.protocol.name=dubbo spring.dubbo.base-package=com.javawxid Logging. Level. The root = info spring. Redis. Host = 192.168.0.100 spring. Redis. Port = 6379 spring. Redis. Database = 0Copy the code

3. Use of YML files

(1) introduction of yml

Yml is a data-centric file based on YAML (YAML Ain’t Markup Language), which is more suitable for configuration files than JSON or XML.

(2) yml grammar

  • Use indentation to indicate hierarchy
  • The Tab key is not allowed for indentation. Only Spaces are allowed.
  • The number of Spaces indented does not matter, as long as elements of the same rank are aligned to the left
  • Case sensitivity

③ Three data structures supported by YAML

  • Object: a collection of key-value pairs
  • Array: A set of values arranged in order
  • Literals: a single, non-divisible value

Syntax format of YML file:

SSL: key-store: keystore.p12 key-alias: tomcat key-store-password: 123456 key-store-type: PKCS12 spring: datasource: url: jdbc:mysql://localhost:3306/mall? useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 druid: Initial-size: 5 # Initial pool size Min-idle: 10 # Minimum number of idle connections max-active: 20 # Maximum number of connections Web-stat-filter: exclusions: *.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" druid login-password: druid data: mongodb: host: localhost port: 27017 database: mall-port redis: host: Database: 0 # Redis database index (default: 0) port: 6379 # Redis server connection port: # Redis server connection password (default null) jedis: pool: max-active: 8 -1ms # Maximum waiting time for connection pool blocking (negative value indicates no limit) max-idle: 8 # Maximum idle connection in connection pool min-idle: 0 # Minimum idle connection in connection pool timeout: 3000ms # Connection timeout (ms) RabbitMQ: host: 192.168.0.135 Port: 5672 Virtual-host: /mall username: mall password: # mall publisher - confirms: true if the asynchronous messaging needs correction must be set to true # log configuration logging: level: org. Springframework. Data. The mongo. Core: debug com.macro.mall.mapper: debug com.macro.mall.portal.dao: debugCopy the code

Common SpringBoot dependencies

pom.xml

<! To be a spring boot project, you must first inherit spring-boot-starter-parent from pum. XML. GroupId >org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 1.5.8. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <! -- Environment parameters. In a normal Maven project, you need to configure plug-ins in POm. XML to change JDK versions, UTF-8 encoding, and other environment parameters. In Eclipse, press CTRL + left click on spring-boot-starter-parent. Look at its source - > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > <! - compile a character encoding to utf-8 -- > < project. Reporting. OutputEncoding > utf-8 < / project. Reporting. OutputEncoding > <! -- Output character encoding utF-8 --> <java.version>1.8</java.version><! </properties> <dependencies> <! Core dependencies, including Auto-Configuration, logging, and YAML. -- > <! -- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>--> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! -- SpringMVC, which stands for Web module, contains many JAR packages in this module, including Spring related JAR, built-in Tomcat server, Jackson, etc. The functions commonly used in web projects are automatically introduced --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <! MyBatis --> <dependency> <groupId>org.mybatis. Generator </groupId> <artifactId> The < version > 1.3.3 < / version > < / dependency > <! -- MyBatis--> <dependency> <groupId>org. MyBatis </artifactId> <version>3.4.6</version> </dependency> <! --> <dependency> <groupId> Mysql </groupId> <artifactId>mysql-connector-java</artifactId> The < version > 8.0.15 < / version > < / dependency > <! DruidDataSource--> <! Alibaba </groupId> <artifactId>druid-spring-boot-starter</artifactId> The < version > 1.1.10 < / version > < / dependency > < / dependencies > <! Maven generates executable JAR files. By default, jar files generated by Maven packages are used for dependencies on other projects and cannot be run directly. Spring Boot provides a plug-in to generate executable JAR files for your own needs. Spring-boot-starter-parent can be found in the source code --> <build> <! -- Access path in browser, if you change it to HelloWorld, then execute Maven &#45; & # 45; Update, then run the project access path is http://localhost:8080/helloworld/ rather than the http://localhost:8080/test-- project name > <! --<finalName>demo</finalName>--> <! Declare this plug-in in your project's pom.xml and it will take effect --> <plugins> <! Maven plugin --> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build>Copy the code