SpringBoot core ① - getting started
Based on the compilation of teacher Lei Fengyang's tutorial
brief introduction
A framework to simplify Spring application development;
A large integration of the whole Spring technology stack;
One stop solution developed by J2EE;
advantage
- Quickly create independent Spring projects and integrate with mainstream frameworks
- Using the embedded Servlet container, the application does not need to be packaged into a WAR package
- starters automatic dependency and version control
- A large number of automatic configurations can simplify development, and the default values can also be modified
- No XML configuration, no code generation, out of the box
- Runtime application monitoring in quasi production environment
- Integration with natural cloud computing
Microservices
2014,martin fowler
Microservices: architecture style (service miniaturization)
An application should be a group of small services; Interworking can be carried out through HTTP;
Single application: ALL IN ONE
Microservices: each functional element is ultimately a software unit that can be replaced and upgraded independently;
Refer to the microservice documentation for details
Going to a restaurant for dinner is a complete business. The chefs, garnishes, waiters and waiters of the hotel are distributed; There is more than one chef, garnish, waiter and waiter, which is the cluster; Distributed is a form of micro service. Distributed is the deployment level and micro service is the design level
Environmental constraints
–jdk1.8: Spring Boot recommends jdk1 7 and above;
–maven3.x: Maven version 3.3 or above;
Maven settings
Settings for maven Add the profiles tag of the XML configuration file: (set the jdk version to use)
maven in the development tool is set as the maven configured by yourself
`<profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>`Copy to clipboardErrorCopied
Create a maven project
-
Import spring boot related dependencies
`<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>`Copy to clipboardErrorCopied
-
Write a main program; Start the Spring Boot application
`package cn.clboy.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Author cloudlandboy * @Date 2019/11/13 2 pm:58 * @Since 1.0.0
*/ @SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //start-up SpringApplication.run(HelloWorldMainApplication.class, args); } }`Copy to clipboardErrorCopied ```
-
Write a Controller
`package cn.clboy.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author cloudlandboy * @Date 2019/11/13 3 pm:05 * @Since 1.0.0
*/ @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello SpringBoot,this is my first Application"; } }`Copy to clipboardErrorCopied ```
- Run the Main method test of the Main program
- visit localhost:8080/hello
Simplify deployment
-
Add maven plug-in
`<!-- This plug-in can package the application into an executable jar Package;--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>`Copy to clipboardErrorCopied
- Packaging with mvn package
- Enter the directory where the packaged jar package is located
- Run with the Java - jar package name
Hello World exploration
rely on
`<!--Hello World The parent project of the project is org.springframework.boot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <!-- org.springframework.boot His parent project is spring-boot-dependencies He came to really manage Spring Boot All dependent versions in the application; Spring Boot The version of the Arbitration Center; In the future, we will import dependency. By default, we don't need to write a version; (not in) dependencies The dependency managed in it naturally needs to declare the version number) --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.1.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>`Copy to clipboardErrorCopied
starter
`<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>`Copy to clipboardErrorCopied
spring-boot-starter-web:
Spring boot starter: spring boot scenario initiator; Help us import the components that the web module depends on for normal operation;
Spring Boot extracts all functional scenarios and makes them into starters. It only needs to introduce these starters into the project, and all dependencies of relevant scenarios will be imported. You can import the initiator of any scene with any function you want
Main program class
`@SpringBootApplication public class HelloWorldMainApplication { public static void main(String[] args) { //start-up SpringApplication.run(HelloWorldMainApplication.class, args); } }`Copy to clipboardErrorCopied
@SpringBoot application: the spring boot application is marked on a class to indicate that this class is the main configuration class of SpringBoot. SpringBoot should run the main method of this class to start the SpringBoot application;
Take a look at the source code of @ SpringBootApplication annotation class
`@Target({ElementType.TYPE}) //You can annotate a type, such as class, interface and enumeration @Retention(RetentionPolicy.RUNTIME) //It can be kept until the program runs, and it will be loaded into the JVM @Documented //Include the elements in the annotation into Javadoc. @Inherited //Inheritance. For example, class A has the annotation, class B inherits class A, and class B also has the annotation @SpringBootConfiguration @EnableAutoConfiguration /* *Create a configuration class and add @ ComponentScan annotation on the configuration class. *This annotation will scan all configuration classes under the package where this class is located by default, which is equivalent to the previous < context: component scan >. */ @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication`Copy to clipboardErrorCopied
-
@SpringBootConfiguration: configuration class of Spring Boot; Marked on a class, indicating that it is a Spring Boot configuration class;
`@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration`Copy to clipboardErrorCopied
-
@Configuration: mark this annotation on the configuration class;
Configuration class - configuration file; The configuration class is also a component in the container@ Component
`@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration` Copy to clipboardErrorCopied
-
-
@EnableAutoConfiguration: enable the auto configuration function;
Spring Boot helps us configure things we need to configure before@ EnableAutoConfiguration tells SpringBoot to enable the auto configuration function; Only in this way can the automatic configuration take effect;
`@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration`Copy to clipboardErrorCopied
-
@AutoConfigurationPackage: autoconfiguration package
`@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import({Registrar.class}) public @interface AutoConfigurationPackage`Copy to clipboardErrorCopied
-
@Import: Spring's bottom annotation @ import imports a component into the container
Imported components by`org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar`Configure the main configuration class(@SpringBootApplication All components in the package and all sub packages below the marked class) are scanned to Spring Containers;
here controller The package is under the package where the main program is located, so it will be scanned. We springboot Create one under package test Package, put the main program in test Under the package, so the startup will only scan test Content under package controller The package will not be scanned before accessing the beginning hello It's 404 
-
- @Import({AutoConfigurationImportSelector.class})
AutoConfigurationImportSelector.class returns all components to be imported in the form of full class name; These components will be added to the container; Many automatic configuration classes (xxxAutoConfiguration) will be imported into the container; Import all the components needed for this scenario into the container and configure them;
The automatic configuration class eliminates the need for us to manually write configuration injection function components;
-
==Spring Boot starts from meta-inf / spring. In the classpath Obtain the values specified by EnableAutoConfiguration from factories, import these values into the container as automatic configuration classes, and the automatic configuration class will take effect to help us with automatic configuration== In the past, we needed to configure things ourselves, and the automatic configuration class helped us;
The overall integration solution and automatic configuration of J2EE are in spring-boot-autoconfigure-1.5.9 RELEASE. jar;
Use Spring Initializer to quickly create Spring Boot projects
IDE supports the use of Spring's project creation wizard to quickly create a Spring Boot project;
Select the module we need; The wizard will create a Spring Boot project online;
Need networking
IDEA
- Select Spring Initializr when creating the project
- Improve project information
The reason for the appearance of Artifact contains illegal characters is that uppercase is used in Artifact, which can only be all lowercase, and words are separated by -
- Select the desired starter
- After the creation, the unwanted files can be deleted. The xml file has automatically configured the dependencies for us. We can configure whatever we have selected before
SpringToolSuite4
- Using STS
- When creating a project, select Spring Starter Project
- Perfect information
- Select the desired starter
Spring Boot project generated by default
- The main program has been generated. We just need to complete our own logic
-
Directory structure in resources folder
- Static: save all static resources; js,css,images;
- templates: save all template pages; (the default jar package of Spring Boot uses embedded Tomcat and does not support JSP pages by default); You can use template engines (freemaker, thymeleaf);
- application.properties: configuration file of Spring Boot application; You can modify some default settings;