Super detailed spring boot introductory notes (summary)
1. Getting started with spring boot
Spring boot is a relatively new project in the spring community. The purpose of the project is to help developers create spring based applications and services more easily, so that more people can get started with spring faster, and java development can achieve the productivity of Ruby on rails. It provides a fixed framework for the spring ecosystem with convention over configuration style.
Spring boot has the following features:
1.1 simple example
First, create a general Maven project with a POM XML and the basic Src / main / Java structure.
1.1. 1 pom. XML file
1.1. 2 description of POM
The first is to add. Adding parent POM is relatively simple, and spring boot starter parent contains a large number of configured dependency management. You do not need to write the version number when adding these dependencies to your project.
Although it is simple to use the parent POM, in some cases we already have a parent POM and cannot add it directly, you can do so in the following ways:
1.1. 3 about Java Version attribute
Above POM Although this attribute does not appear in XML, you should be reminded here.
Spring uses jdk1.0 by default 6. If you want to use jdk1 8. You need to be in POM Add Java. XML to the XML attribute Version, as follows:
1.1. 4 add spring boot starter web dependency
Spring can support a specific function by adding dependencies such as spring boot starter - *.
Our example is ultimately to implement web functions, so we add this dependency.
1.1. 5 add spring boot Maven plugin
The plug-in supports a variety of functions. There are two commonly used. The first is to package the project as an executable jar package.
Executing MVN package in the project root directory will generate an executable jar package, which contains all dependent jar packages. Only this jar package is needed to run the program, which is very convenient to use. After the command is executed, an XXX will be retained jar. The original jar package contains separate parts of the project.
After generating the executable jar package, execute Java - jar XXXX. Exe on the command line Jar to start the project.
Another command is MVN spring boot: run. You can directly use Tomcat (default) to start the project.
In our development process, we need to modify it frequently. In order to avoid repeated startup of projects, we can enable hot deployment.
1.1. 6 spring loaded hot deployment
Spring loaded project provides a powerful hot deployment function. It can be hot deployed when adding / deleting / modifying methods / fields / interfaces / enumerations and other codes, which is fast and convenient.
It is very simple to use this function in spring boot by adding dependencies under the spring boot Maven plugin plug-in:
After the addition, hot deployment is supported through MVN spring boot: run startup.
Note: when using hot deployment, you need the IDE to compile the class before it takes effect. You can turn on the automatic compilation function, so that the class will be automatically reloaded when you save the changes.
1.2 create an application class
We create an application class:
1.2. 1 note
Spring boot suggests configuring the main configuration class where our main method is located under the root package name.
In application There is a main method in Java.
Because of the default package related annotations, the default package name is the package of the current class, such as @ componentscan, @ entityscan, @ springbootapplication annotations. (all packages installed in the current application.java are scanned as scan)
1.2. 2 @RestController
Because our example is to write a web application, this annotation is equivalent to adding @ controller and @ ResponseBody annotations at the same time.
1.2. 3 @EnableAutoConfiguration
Spring boot recommends only one class with this annotation.
@Enableautoconfiguration function: spring boot will automatically configure the project according to the dependency of your jar package.
For example, when you have a dependency on HSQLDB under your project, spring boot will create the default datasource of the memory database. If you create a datasource yourself, spring boot will not create the default datasource.
If you don't want spring boot to be created automatically, you can configure the exclude attribute of the annotation, for example:
1.2. 4 @SpringBootApplication
Because a large number of items will be added on the main configuration class
@Configuration, @ enableautoconfiguration, @ componentscan.
Therefore, spring boot provides the @ springbootapplication annotation, which can replace the above three annotations (implemented using spring annotation inheritance).
1.2. 5 start the project springapplication run
The easiest way to start the spring boot project is to execute the following methods:
This method returns an ApplicationContext object. When using annotations, the specific type returned is annotationconfigapplicationcontext or annotationconfigembeddedwebapplicationcontext. When supporting web, it is the second.
In addition to the above methods, the following methods can be used:
Spring application contains some other configurable methods. If you want to do some configuration, you can use this method.
In addition to the direct method above, you can also use spring application builder:
When using spring MVC, you need to use spring applicationbuilder because you need to use sub containers. This class has a child (XXX...) method to add sub containers.
1.3 operation
Execute the main method directly in the IDE, and then access http://localhost:8080 Just.
In addition, you can also use the MVN mentioned above, which can be packaged into an executable jar package, and then execute Java - jar XXX jar。
Or execute MVN spring boot: run to run the project.
2. Spring boot attribute configuration and use
Spring boot allows you to use the same application code in different environments through external configuration. In short, you can inject properties or modify the default configuration through the configuration file.
2.1 spring boot supports multiple external configuration modes
The priorities of these methods are as follows:
2.1. 1 command line parameters
Through Java - jar app jar Cname=”Spring” Cserver. Port = 9090 to pass parameters.
The parameter is passed in the form of Cxxx = XXX.
The parameters that can be used can be defined by ourselves or the default parameters in spring boot.
Many people may be concerned about how to configure the web port. These are the parameters provided in spring boot. Some of the available parameters are as follows:
Note: the command line parameters are in app Behind the jar!
You can use springapplication Setaddcommandlineproperties (false) disables command line configuration.
2.1. 2 java system properties
Note the Java system attribute location Java - dName = "isea533" - jar app Jar, the configurable properties are the same, with different priorities.
For example, Java - dName = "isea533" - jar app jar Cname=”Spring!” The name value in is spring!
2.1. 3 operating system environment variables
Configured Java_ Everyone in home should know this one.
It should be noted that some OS can not be used This name, such as server Port, server can be used in this case_ Port.
2.1. 4 RandomValuePropertySource
Where random numbers are used in the system, for example:
random. Int * supports the value parameter and the max parameter. When the max parameter is provided, value is the minimum value.
2.1. 5 application configuration file (. Properties or. YML)
Write directly in the configuration file:
. Configuration files in YML format, such as:
When there is a prefix, use The configuration file in YML format is simpler. About See here for the usage of YML configuration file
Note: use YML, there must be a space between the value of the attribute name and the colon. For example, if name: isea533 is correct, name: isea533 is wrong.
2.1. 5.1 location of attribute profile
Spring will look for application.config from the / config directory under the classpath or the root directory of the classpath Properties or application yml。
/Config takes precedence over the classpath root directory
2.1. 6 @PropertySource
This annotation can specify a specific attribute configuration file with low priority.
2.1. 7 SpringApplication. setDefaultProperties
For example:
Spring boot will automatically inject the attribute prefix = "my" prefixed with my.
Spring boot will automatically convert types. When using list, you should pay attention to initializing the list in the configuration!
Spring boot also supports nested attribute injection, for example:
Corresponding configuration class:
All properties starting with JDBC are injected into the JDBC object.
2.2. 3 use @ configurationproperties on the @ bean method
For example:
Spring boot will inject the attributes beginning with foo into the foocomponent object according to name matching.
2.2. 4 property placeholder
For example:
You can refer to the previously configured attributes in the configuration file (priority can be used here).
You can also set the default value through the ${app. Name: default name} method. When the referenced attribute cannot be found, the default attribute will be used.
Because the ${} method will be handled by Maven. If you inherit spring boot starter parent from POM,
Spring boot has changed the default ${} mode of Maven resources plugins to @ @ mode, such as @ name @.
If you are introducing spring boot, you can modify and use other separators
2.2. 5. The command parameters can also be shortened through the attribute placeholder
For example, to modify the default web port, you need to use cserver Port = 9090 mode, if it is written in the configuration:
Then you can use a shorter CPort = 9090, and use the default value of 8080 when this parameter is not provided.
2.2. 6 attribute name matching rules
For example, there are the following configuration objects:
Firstname can use the following property names:
person. Firstname, standard hump naming
person. First name, dashed (-) division, recommended in. Properties and. YML configuration files
PERSON_ FIRST_ Name, in uppercase and underlined form, is recommended to be used in the system environment variable
2.2. 7 attribute validation
The jsr-303 annotation can be used for verification, for example:
2.3 final
The above is the content of spring boot attribute configuration and use. For some incompleteness or readers have more questions, you can check the complete spring boot documentation or externalized configuration.
3. Spring boot integrates mybatis
3.1. Spring boot integrated Druid
Druid has many configuration options. You can easily configure Druid by using the configuration file of spring boot.
In application Write in the YML configuration file:
Here via type: com alibaba. druid. pool. Druiddatasource can be configured!
3.2. Spring boot integrates mybatis
There are two ways for spring boot to integrate mybatis. One simple way is to use the official mybatis:
mybatis-spring-boot-starter
Another way is to still use the configuration method similar to mybatis spring. This method needs to write some code, but it can easily control the configuration of mybatis.
3.2. 1. Mybatis spring boot starter mode
In POM Add dependency to XML:
The dependency tree of mybatis spring boot starter is as follows:
Mybatis uses 3.3 Version 0, you can:
In addition to the above two common configurations, there are:
3.2. 2. Mybatis spring mode
This way is close to the usual usage. You need to add mybatis dependency and mybatis spring dependency.
Then create a mybatisconfig configuration class:
The above code creates an sqlsessionfactory and an sqlsessiontemplate. In order to support annotation transactions, the @ enabletransactionmanagement annotation is added, and a platformtransactionmanagerbean is returned.
In addition, it should be noted that there is no mappercannerconfigurer in this configuration. If we want to scan the mapper interface of mybatis, we need to configure this class. This configuration needs to be placed in a separate class.
For this configuration, you must pay attention to @ autoconfigureafter (mybatisconfig. Class). This configuration must be available, otherwise there will be exceptions. The reason is that this class is executed early. Since sqlsessionfactory does not exist, subsequent execution errors occur. After completing the above configuration, you can use mybatis.
3.3. About paging plug-in and general mapper integration
An example of a paging plug-in as a plug-in is shown in the code above.
The general mapper configuration is actually used when configuring mapperscannerconfigurer
tk. mybatis. spring. mapper. Mappercannerconfigurer can be used to configure properties.
3.4. Spring boot is a basic project for integrating mybatis
Project address: https://github.com/wudongqiang/MyBatis-Spring-Boot
WebJars: http://www.webjars.org/