Take you to a quick start with Spring Boot in five minutes

Take you to a quick start with Spring Boot in five minutes

1 Introduction

​ a) Simplify the framework of spring application development; b) a large integration of the entire spring technology stack; c) a one-stop solution for J2EE development;

Advantages:

1. Quickly create independent running spring projects and integrate with mainstream frameworks

​ 2. Using an embedded servlet container, the application does not need to be packaged as war

​ 3. Starters automatic dependency and version control

​ 4. A large number of automatic configurations to simplify development, and you can also modify default values

​ 5. Disordered configuration XML, no code generation, ready to use out of the box

​ 6. Runtime application monitoring in quasi-production environment

​ 7. Natural integration with cloud computing

2. Build the environment

A) maven provided: to label the maven profiles settings.xml configuration file is added

<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>

b) Idea settings: Configure maven on your idea tool

Insert picture description here

3. SpringBoot creates a simple project

Demand: The browser sends hello request, the server accepts and processes the request, a response string Hello World

​ a) Create a maven project (jar)

​ b) Import spring boot related dependencies

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

​ c) Write a main program; start the Spring Boot application

//@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
@SpringBootApplication
public class HelloWorldMainApplication {
    
    
    public static void main(String[] args) {
    
    
        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

​ d) Write related Controller

@Controller
public class HelloController {
    
    
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
    
    
        return "Hello World!";
    }
}

​ e) Start the main method in the main program, enter the address from the browser to access (the server will return "Hello World!")


​ f) Simplified deployment: This application can be marked as a jar package and executed directly with the java -jar command;

 <!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

4. Go deep into the bottom of the springBoot project

a) POM file

① Parent Project

<!--SpringBoot项目依赖的的父项目-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

<!--父项目再依赖的父项目如下,它是真正管理Spring Boot应用里面的所有依赖版本-->
<!--以后我们在导入依赖时,默认是不需要写版本号的(若不在dependencies里面管理的依赖,需要声明版本号)-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>1.5.9.RELEASE</version>
  <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

② starter

Spring Boot extracts all the functional scenarios into starters (starters). All the dependencies of these starter-related scenarios need to be imported into the project. Import the launcher of the scene for what function you want, for example:spring-boot-starter-web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <!--spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;-->
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

b) Main program class, main entrance class

//@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
@SpringBootApplication
public class HelloWorldMainApplication {
    
    
    public static void main(String[] args) {
    
    
        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

@ SpringBootApplication (Spring Boot configuration class)

​ The Spring Boot application annotation on a certain class indicates that this class is the main configuration class of SpringBoot. SpringBoot should run the main method of this class to start the SpringBoot application. Check the details of this annotation below:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration//标注在某个类上,表示这是一个Spring Boot的配置类;
@EnableAutoConfiguration//开启自动配置功能
@ComponentScan(excludeFilters = {
     
     
      @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
      @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
     
     }//@SpringBootApplication注解

	//------------------------------------------------------------------------
    @Target({
     
     ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration//代表配置类(全注解开发)== 配置文件
    public @interface SpringBootConfiguration {
     
     }//@SpringBootConfiguration注解


		//------------------------------------------------------------------------
        @Target({
     
     ElementType.TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Documented
        @Component//配置类也是容器中的一个组件
        public @interface Configuration {
     
     //@Configuration注解
            String value() default "";
        }

@ EnableAutoConfiguration (Enable the automatic configuration function)

​ Before we need to configure the content, Spring Boot helps us automatically configure; @ EnableAutoConfiguration tells SpringBoot to turn on the automatic configuration function; so that the automatic configuration can take effect, see the details of this annotation below:

@AutoConfigurationPackage//自动配置包
@Import(EnableAutoConfigurationImportSelector.class)//Spring的底层注解@Import,是将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器
public @interface EnableAutoConfiguration {
     
     }

When Spring Boot starts, it obtains the values ​​specified by EnableAutoConfiguration from META-INF/spring.factories under the classpath, and imports these values ​​into the container as the auto-configuration class. The auto-configuration class will take effect and help us with the auto-configuration work; In the past, we needed to configure things by ourselves, the automatic configuration class helped us (it can be said that the springBoot official has done these tasks), the overall J2EE integration solution and automatic configuration are all in spring-boot-autoconfigure-1.5.9.RELEASE.ja

5) Use Spring Initializer to quickly create a Spring Boot project

​ ① IDEA: Use Spring Initializer to quickly create a project

​ IDEs support 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 on the Internet, and the default generated Spring Boot project;

- 主程序已经生成好了,我们只需要我们自己的逻辑
- resources文件夹中目录结构
  - static:保存所有的静态资源; js css  images;
  - templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
  - application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/107140300