Start Spring Boot Tour 1, Spring boot introduction
A framework for simplifying spring application development;
A large integration of the entire spring technology stack;
One-stop solution for Java EE development.
2. Micro-Service
Martion Fowler A long article https://martinfowler.com/microservices/, 2014; Although he is not a micro-service provider, his article clearly defines what microservices are.
MicroServices: Architectural style
An application should be a small set of services that can be communicated via HTTP.
-
Single Application (non-microservices)ALL IN ONE
Everything is written in one application. Development of an OA CRM ERP system, an application packaged into a war package placed on Tomcat and other servers directly run. Development testing is simple and deployment is simple. The extension is also simple. If the load does not work, copy more than 10 of the same applications on more than 10 servers and increase our concurrency capabilities through load balancing mechanisms.
The problem is: reaching; the bigger the project, the more problems.
-
Micro-Service
Each functional element is ultimately a software unit that can be independently replaced and upgraded independently;
See MORE: Chinese documents (translation of Martinfowler articles ( ̄▽ ̄) ~( ̄▽ ̄) ~)
-
Build large distributed applications
Service Micro is a large-scale distributed application, in the face of such a large-scale distributed application how we build the application of Spring's official website A classic gives us the idea of first using spring Boot to build applications quickly. Use spring cloud for distributed mesh interconnect intermodulation. Stream data is calculated in batches using Springcloud Date flow in the distributed middle.
3. Spring Boot Quirk Start
-
https://start.spring.io/
Use spring INITIALIZR to quickly build a spring Boot project. Or use tools like spring's official integrated development environment STS and IntelliJ idea to quickly build a spring boot project.
-
The following directory structure is automatically generated for us through spring INITIALLIZR:
|-demo
? |-src
? |-main
? |-java
? |-com.example.demo
? |-demoapplication.java//Startup class
? |-resources
? |-static//Placing static resources
? |-templates//Front-end templates
? |-application.properties//config file
? |-test
? |-java
? |-com.example.demo
? |-demoapplicationtests.java//Test startup class
? |-target//program will be here after packing?
4. Spring Boot Startup class
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @SpringBootApplication 来、标注一个主程序类,来说明这是一个Spring Boot 应用 */@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { //Spring应用启动起来 SpringApplication.run(DemoApplication.class, args); }}
@SpringBootApplication: The Spring boot app callout on this class shows that this class is the main configuration class for spring boot.
Spring boot should run the main method of this class to start the Spring boot application, which opens as follows:
@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })public @interface SpringBootApplication {
@SpringBootConfiguration: Spring Boot Configuration class;
? The callout on the touch class indicates that this is a Spring Boot configuration class, and he opened it with a @configuration annotation.
? @Configuration: This annotation is annotated on the configuration class;
? The configuration class----The corresponding-----configuration file, and the configuration class is also a component in the container @Component
?
@EnableAutoConfiguration: turn on the automatic configuration feature
? We used to configure things ourselves, and spring Boot helped us configure them automatically, and @EnableAutoConfiguration added on the configuration class, the automatic configuration would take effect. He opened it, and your face was a combination of @autoconfigurationpackage and @import notes:
@AutoConfigurationPackage@Import(AutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration {
@AutoConfigurationPackage: Annotations for automatic configuration packages
@Import(AutoConfigurationPackages.Registrar.class)public @interface AutoConfigurationPackage {
? Spring's underlying annotation @import, importing a component into the container; The imported component is specified by Autoconfigurationpackages.==registrar==.class. Look at this method under this class:
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { register(registry, new PackageImport(metadata).getPackageName());}
Let's debug:
As a result, the @AutoConfigurationPackage annotation is to scan the package of the main configuration class (@SpringBootApplication the class being labeled) and all the components in all the sub-packages below to the spring container.
@Import ({org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.class})
What kind of components are imported to the container to import the components?
Autoconfigurationimportselector: The selector of which components to import, enter the class we'll look at a method:
public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader .loadMetadata(this.beanClassLoader); AnnotationAttributes attributes = getAttributes(annotationMetadata); List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); configurations = removeDuplicates(configurations); Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return StringUtils.toStringArray(configurations);}
Debug a bit:
, and all components that need to be imported are returned in a full-class name, and the components are added to the container;
Will import very many automatic configuration classes (xxxautoconfiguration) into the container: all the components needed to import the scene into the container, and configure these components, eliminating the work of our handwritten configuration injection function components.
List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
Parameter Enableautoconfiguration.class,classloader
Spring boot Gets the value specified by enableautoconfiguration from meta-inf/spring.factories in the classpath at startup, and imports these values as automatic configuration classes into the container, and the automatic configuration class takes effect.
In fact, the configuration of the bottom of spring is not a few, but these automatic configuration to help us do.
Java EE's overall integration solution and Autoconfiguration are in: Spring-boot-autoconfigure-2.0.4.release.jar
5. pom.xml file 1, parent project
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent>他是所用spring-boot-starter的父项目。而他打开后,他的父项目是:<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath></parent>他来真正的管理Spring Boot应用里面的所有依赖版本
That is, the Spring Boot version of the Arbitration Center:
So, the dependencies we import in Spring Boot, which are not required by default, are spring-boot-dependencies managed on a dependent version. "Instead of having to spring-boot-dependencies make a dependent version declaration in the natural need to write a version."
2. Import Dependency
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>
spring-boot-starter- web :
Spring-boot-starter:spring Boot Scenario Launcher
Spring-boot-starter-web: Help us to import the components on which the Web module is functioning, and of course the versions are moderated by the parent project.
Spring boot extracts all of the feature scenarios into a single starters (launcher), only to introduce these starter in the project, and all dependencies on the relevant scenarios are imported. What feature genes to use to import the launcher for what scenario. For detailed reference, the Spring boot Help document page searches for starter. To see the initiators of various scenarios, as well as the various dependencies they have imported.
3. Deployment related
<!--将应用打包成一个可执行的jar包--><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>
4. Other Miscellaneous
<groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version></properties>
Contains configuration information for package name, project name, version, package type, name, description, and encoding, JDK version.
6. Three ways to start the Spring boot project
-
Direct launch in IDE
-
CMD Enter project directory type mvn spring-boot:run to start the project
-
Through learning we know that the springboot built into a tomcat server, the package generated jar package can run directly, we mvn install compile the package by command, under the target directory. Enter the directory directly java -jar demo-0.0.1-SNAPSHOT.jar to start the Springboot project.
Start the Spring Boot tour