A, SpringBoot-- entry Helloworld

1 SpringBoot Introduction
Spring Boot Spring to simplify application development, agreed greater than the configured, from simple to complicated, just run will be able to create a separate, product-level applications

Background: heavy development J2EE, various configurations, low development efficiency, complex deployment process, third-party technology integration difficult.

Solve
"Spring family bucket" era.
Spring Boot ---- J2EE-stop solution for
Spring Cloud ----- distributed a total solution

Pros:
quickly create stand-alone projects, and Spring Framework integration with mainstream
use embedded Servlet container, applications do not need to be labeled WAR package (jar packaging direct command run)
Starters automatic dependent and version control
a large number of auto-configuration, simplifying development, but also default values may be modified
without the XML configuration, no code is generated, out-of-
run production-environment monitoring application
and integration of natural cloud

Summary
simplify a Spring application development framework;
a large integration of the entire Spring technology stack;
the J2EE developers a one-stop solution;

2 micro-services
2014, martin fowler
microService: Architecture Style (micro-based service)
application should be a small set of services; can communicate by way of HTTP;
monomer Applications: ALL IN ONE
micro services: each functional element eventually It is an independent alternative and independent software upgrade unit;
reference micro-service document
( https://martinfowler.com/articles/microservices.html#MicroservicesAndSoa)
Here Insert Picture Description
. 3 New SpringBoot Helloworld Demo

Environmental constraints
- jdk1.8
- maven3.x
- IntelliJ IDEA 2017
- the Spring the Boot 1.5.9.RELEASE

MAVEN provided;
to label the maven settings.xml profiles profile Add

JDK 1.8-

to true
1.8


<maven.compiler.source> 1.8 </maven.compiler.source>
<maven.compiler.target> 1.8 </maven.compiler. target>
<maven.compiler.compilerVersion> 1.8 </maven.compiler.compilerVersion>

Method 1: Use spring template (spring initializr) select the jar package import wizard will automatically create a network to create a Spring Boot project (Network flow);
Second way: manually create

  1. Create a maven web project

  2. 引入starters 依赖

    org.springframework.boot
    spring‐boot‐starter‐parent
    1.5.9.RELEASE



    org.springframework.boot
    spring‐boot‐starter‐web

  3. Creating the main program

/**

  • @SpringBootApplication annotate a main class, which is a description Spring Boot application
    * /
    @SpringBootApplication
    public class HelloWorldMainApplication {
    public static void main (String [] args) {
    // Start up Application of the Spring
    SpringApplication.run (HelloWorldMainApplication.class, args );
    }

4) related to the preparation of the Controller, Service
@Controller
public class HelloController {
@ResponseBody
@ RequestMapping ( "/ the Hello")
public String the Hello () {
return "! The Hello World";
}
}
5) start to run and test the
localhost: 8080 / hello

6) simplifies the deployment
<- this plugin can be packaged into an executable apply packet jar;! ->



org.springframework.boot
Spring-Boot-Maven-plugin


This application will be labeled jar package (click on the package in package engineering)
Here Insert Picture Description
copies according to package log shows the path to the desktop, the direct use of java -jar command execution
Here Insert Picture Description
result browser to access the
Here Insert Picture Description
open project packaged jar file you can see which contains springboot the tomcat and other related package, there is no need to manually set up tomcat environment
Here Insert Picture Description

4, HelloWorld inquiry

1) POM file dependent parent project

org.springframework.boot
the Spring-the Boot-Starter-parent
1.5.9.RELEASE

His father project is

org.springframework.boot
the Spring-the Boot-the Dependencies
1.5.9.RELEASE
... / ... / the Spring-the Boot-the Dependencies

the Spring-the Dependencies is the Boot-to really manage Spring Boot application which all depend on the version;
it is Spring Boot version arbitration Center; later we import dependence is no need to write the default version (no need to declare a natural version number which rely dependencies management)
Here Insert Picture Description

2) starters starter

maven dependence

org.springframework.boot spring‐boot‐starter‐web

-the Boot-Starter-the Spring web:
the Spring-the Boot-Starter: the Boot scene the Spring-starter; help us to import the components of web module is operating normally rely on (tomcat, web, etc., F3 shortcut to view detailed dependent);
Here Insert Picture Description
the Spring the Boot All the features are extracted scene, made a number of starters (starter), only you need to import the starter project inside

相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器
Spring Boot为我们提供了简化企业级开发绝大多数场景的starter pom(启动
器),只要引入了相应场景的starter pom,相关技术的绝大部分配置将会消
除(自动配置),从而简化我们开发。业务中我们就会使用到Spring Boot为
我们自动配置的bean
这些starters几乎涵盖了javaee所有常用场景,Spring Boot对这些场景依赖的
jar也做了严格的测试与版本控制。我们不必担心jar版本合适度问题。
spring-boot-dependencies里面定义了jar包的版本

参考 https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#using-boot-starter

2)入口类和@SpringBootApplication

注解间的关系图
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
a) 程序从main方法开始运行
b) 使用SpringApplication.run()加载主程序类
c) 主程序类需要标注@SpringBootApplication

@SpringBootConfiguration:Spring Boot的配置类;
标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解,这是Spring中的注解;
配置类 ----- 配置文件;配置类是容器中的一个组件(包含@Component);
@Component 容器中的一个组件

@EnableAutoConfiguration是核心注解:开启自动配置,以前我们需要配置的东西,Spring Boot帮我们自动配置
@AutoConfigurationPackage定义默认的包扫描规则,程序启动扫描加载主程序类所在的包以及下面所有子包的组件
@Import(AutoConfigurationPackages.Registrar.class):
Spring的底层注解@Import,给容器中导入一个组件;导入的组件由
AutoConfigurationPackages.Registrar.class将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
@Import(EnableAutoConfigurationImportSelector.class)导入所有的自动配置场景。EnableAutoConfigurationImportSelector:导入哪些组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件
Here Insert Picture Description
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;
SpringFactoriesLoader.loadFactoryNames (EnableAutoConfiguration.class, classLoader); Spring Boot acquired at startup from the META-INF class path / spring.factories EnableAutoConfiguration specified value, these values into a container configuration classes as an automatic, automatic configuration class will take effect automatically configured to work for us; what we need before their own configuration, automatic configuration class to help us to automatically configure; J2EE overall integration solutions and automatic configuration are spring-boot-autoconfigure-1.5.9 in. RELEASE.jar;
Here Insert Picture Description
shown above automatically configure auto-configuration class which contains a lot of functional modules

Automatic Configuration small sum xxxAutoConfiguration
A) in the Spring the Boot deposit is a large number of these classes, the role of these classes is to help us to automatically configure
b) that he will need all the components of this scenario are registered to the vessel and configured
c) they META-INF class path / spring.factories document
d) spring-boot-autoconfigure- 1.5.9.RELEASE.jar contains all scenes to automatically configure the class code
e) these classes are automatically configured for Spring Boot automatic configuration of the essence

Guess you like

Origin blog.csdn.net/m0_38143867/article/details/92701857