Introduction to a,
1. Introduction
Spring Boot is a framework that simplifies Spring development. Used to monitor Spring application development, just Run creates a standalone, production-level application with more conventions than configurations and less complexity. Bytes.
When we use Spring Boot, we only need to configure the corresponding Spring Boot to use all Spring components. In short, Spring Boot is to integrate a lot of excellent framework, we do not have to manually write a bunch of XML configuration and then configure. Essentially, Spring Boot is Spring, and it does all the Spring Bean configuration that you would have done without it.
2. The advantages
3. Single application and micro-service
In a single application, all application modules are written in one application. As a result, the larger the project is, the coupling degree between modules becomes higher and higher. Micro service is a kind of architectural style, with micro service application modules can be deployed alone, the management of the different modules for different operation, different modules generate small service, each functional elements finally can become an independent replace, can you upgrade the function of the unit, between each small service to communicate over HTTP.
4. Core features of Spring Boot
· Micro services:
Spring Boot can be used to generate individual units of microservice functionality
· Automatic configuration:
Spring Boot automatically provides configuration for many application functions common to Spring applications
· Initial dependence:
Tell Spring Boot what functionality it needs, and it can import the required libraries.
· Command Line interface
This is an optional feature of Spring Boot that allows you to complete your application by writing code instead of building a traditional project.
Physical:
Allows you to drill down into a running Spring Boot application.
5. Simple example: Create a HelloWorld project using Maven
Step 1: Start by configuring the Spring Boot dependencies
Org.springframework. boot spring-boot-starter-parent 1.5.9.RELEASE Spring Boot version arbitration center; By default, we don’t need to write versions for future import dependencies. Org.springframework. boot spring-boot-starter-web
Spring – the boot – starter – web:
Spring-boot-starter: indicates the starter in the spring-boot scenario. Helps us import components that our Web module depends on to function properly.
Spring Boot extracts all of the functional scenarios into one specific starter, simply by introducing all of the dependencies related to those starter scenarios into the project. Import the scenario initiator for the function you want to use
· Step 2: Create package structure as shown below
· Step 3: Edit the corresponding class code
· HelloController. Java
@ RestController / / tagging is a Controller class And supports the json data format output public class HelloController {/ / through http://localhost:8080/sayHello to access the method @RequestMapping(“/sayHello”) public String hello(){ return “hello world”; }}
· MainTest. Java
Public class MainTest {public static void MainTest {public static void MainTest {public static void MainTest {public static void MainTest Main (String[] args) {// Use springApplication.run (maintest.class); }}
· Step 4: Run main method
Run the main method, wait for the automatic deployment server to complete, and then access the data by address.
6. Case Analysis — corresponding problems
· Why only one Spring Boot dependency is configured:
· @SpringBootApplication annotation for the entry class:
AutoConfigurationPackage:
Automatically scan the main class marked by the problem @SpringBootApplication, find the corresponding control class under the package or subpackage of the main class, and automatically configure the beans required by the corresponding project according to the corresponding annotations
7. How to quickly create a Spring Boot project
Notice When creating the Spring Boot, ensure that the network connection is normal. Click New File, select Project and select Spring Initializr. After loading, check to see if the appropriate JDK version is available. Then click Next
Set up GroupID and Artifact for the project. Select the appropriate language and the JAR or WAR package and JDK version to generate
Select the dependencies needed to create the project, such as database, Web dependencies, etc., here we briefly introduce, do not do specific details, select a Web dependency
After the selection is complete, click Finish to complete the new project and wait for the project to complete.
Once created, keep the structure of the creation in this form.
After creating the project, simply create a project to test according to the above case, the test can be passed.
8. Configuration file of Spring Boot
Using the Spring Boot configuration file first requires JavaBean classes before assigning values to properties in the JavaBean in the corresponding configuration file
To assign a value to a JavaBean class, add the corresponding annotations @Component and @ConfigurationProperties(prefix = “person”) to the JavaBean class. @Component adds the class to the container, and data from the @ConfigurationProperties configuration file is injected into the class
T, Java
public class Dog {private String name; private String age; }
· Person. Java
/ * *
-
Map the value of each property configured in the configuration file to this component
-
ConfigurationProperties: tells SpringBoot to bind all properties in this class to the relevant configuration in the configuration file.
-
Prefix = “person” : all attributes under which in the configuration file are mapped one by one
-
The @ConfigurationProperties function provided by the container can only be provided if the component is a component within the container.
* * /
@Component@ConfigurationProperties(prefix = “person”) public class Person { private String name; private Integer age; private Boolean sex; private Date birthday; private List list; private Map<String,Object> map; private Dog dog; }
· Configuration file:
The configuration file name is either fixed application.properties or application.yml
· Functions of configuration files:
Change the default SpringBoot automatic configuration. SpringBoot is automatically configured for us at the bottom.
· application. The properties, rounding
Example:
Port =9999 server.port=9999
After configuring the corresponding properties in application.properties, re-run the project and find that the server’s port startup number has changed to 9999, proving that the code can change the default port 8080.
· application. The properties
List =a,b,c person.name= rich person.sex=false person.map.v1=k1 person.map.v2=k2 person.dog.name=jjj person.dog.age=6
Note: Garbled characters may appear when using the application.properties file to the configuration file. Change the encoding format to UTF-8
· application. Yml explanation
·1. Detailed explanation of YAML file
(1) YAML file definition:
YAML Ain’t Market Language (YAML Ain’t Market Language) Language files are more suitable for configuration files than JSON and XML files
(2) Basic syntax of YAML files:
-
Use indentation to indicate hierarchy
-
The Tab key is not allowed for indentation. Only Spaces are allowed
-
The number of indented Spaces does not matter, as long as elements of the same rank are left aligned
-
Case sensitivity
(3) The value of YAML is written as:
-
K: V: write it literally;
-
Strings do not use single or double quotation marks by default.
-
“” : double quotation marks; Does not escape special characters inside the string; Special characters act as their intended meaning
-
Name: “zhangsan \n lisi” : output; Zhangsan newline lisi
-
“: single quotation mark; Special characters are escaped, and the special characters end up just plain string data
-
Name: ‘zhangsan \n lisi’ : output; zhangsan \n lisi
(4) YAML supports three data formats: object, array, and primitive data types
·2. Examples of common writing of YAML
· application. XML
List: a-b map: -k1: v1-k2: v2-dog: {name: ww,age: {name: ww,age: 12} dog: name: ww age: 2
After the corresponding configuration files and classes are written, edit the test class, run it, and the test passes.
Special annotations in Spring Boot
Here is to explain a few commonly used annotations, after all, time is limited, we can check online, specific annotations
@SpringBootApplication annotation analysis
@SpringBootApplication is a composite annotation that includes @ComponentScan, @SpringBootConfiguration, and @EnableAutoConfiguration.
1.@SpringBootConfiguration inherits from @Configuration and has the same functionality, marking the current class as a Configuration class and incorporating one or more instances of methods declared in the current class marked with the @Bean annotation into the srping container, with instance names being method names.
2.@EnableAutoConfiguration enables automatic configuration.
The 3.@EnableAutoConfiguration annotation means that Springboot will configure the default configuration of your project based on the jar packages you add, such as spring-boot-starter-web, To determine if your project needs to add WebMVC and Tomcat, it will automatically configure the default configuration for your Web project. The blog will examine this annotation in detail below, but the quickstart demo didn’t actually use it.
4.@ComponentScan, scan the current package and its subpackages for classes marked by the @Component, @Controller, @Service, and @repository annotations and incorporate them into the Spring container for management. Is the old context: Component-scan (used to use the tag used in XML to scan parallel support for package configuration). So why is the User in this demo managed by the Spring container
@ResponseBody
Indicates that the return result of this method is directly written to the HTTP Response body, which is usually used to obtain data asynchronously and build RESTful apis. After @requestMapping is used, the return value is usually interpreted as a jump path. After @esponSeBody is added, the return value is not interpreted as a jump path and is written directly to the HTTP Response Body. So if you get json data asynchronously, and you add @responseBody, it will return json data directly. This annotation is typically used in conjunction with @requestMapping
@Controller
This is used to define the controller class, which in spring projects is responsible for forwarding URL requests from users to the corresponding service layer. Usually this annotation is in the class, and usually the method needs to be annotated with @requestMapping
@RestController
A collection of annotated control-layer components (such as actions in Struts), @responseBody, and @Controller.
@Service
Typically used to modify components in the Service layer
@Repository
Using the @Repository annotation ensures that DAO or Repositories provides an exception translation. The DAO or Repositories class modified by this annotation will be found and configured by ComponetScan without providing an XML configuration entry for them.
@Bean
Using the @bean annotation method is equivalent to configuring beans in XML.
@Value
Inject the value of the Spring Boot Application.properties configured property.
This article turns to music bytes.