Loading

SpringBoot入门

1 简介

  • SpringBoot简化Spring应用开发,约定大于配置,去繁从简,just run就能创建一个独立的、产品级别的应用。

  • 背景:J2EE笨重的开发、繁多的配置、低下的开发效率、复杂的部署流程、第三方技术集成难度大。

  • 解决:

    • “Spring全家桶”时代。
    • SpringBoot-->J2EE一站式解决方案。
    • SpringCloud-->分布式整体解决方案。
  • 优点:

    • 快速创建独立运行的Spring项目以及与主流框架集成。
    • 使用嵌入式的Servlet容器,应用无需打成war包。
    • starters自动依赖和版本控制。
    • 大量的自动配置,简化开发,也可修改默认值。
    • 无需配置XML,无代码生成,开箱即用。
    • 准生产环境的运行时应用监控。
    • 和云计算的天然集成。

2 微服务

  • 2014年,Martin Flower。
  • 微服务,是一种架构风格。
  • 一个应用应该是一组小型服务。可以通过HTTP的方式进行互通。
  • 每一个功能元素最终都是一个可独立替换和独立升级的软件单元。

3 入门案例

3.1 环境准备

  • JDK 1.8

  • Maven 3.x

  • IDEA最新版

  • SpringBoot 1.5.9.RELEASE

  • maven设置JDK的版本:

  • ①在Maven的conf/settings.xml中的profiles节点中添加

<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>
  • ②在Maven构建的项目的pom.xml中的<build></build>节点的<plugins></plugins>添加
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
    </configuration>
</plugin>

3.2 SpringBoot HelloWorld

3.2.1 功能需求

  • 浏览器发送hello请求,服务器接收请求并处理,响应输出Hello World字符串。

3.2.2 创建Maven工程(jar)

  • 略。

3.2.3 导入SpringBoot的相关依赖jar包的Maven坐标

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sunxiaping</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0</version>
    <name>springboot</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.2.4 编写主程序

  • SpringBootApplication.java
package com.sunxiaping.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 主程序,用来启动SpringBoot应用
 */
@SpringBootApplication
public class HelloWorldAApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldAApplication.class, args);
    }

}

3.2.5 编写相关的Controller

  • HelloController.java
package com.sunxiaping.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "Hello World";
    }

}

3.2.6 运行测试

  • 只需要运行SpringbootApplication.java即可。

3.2.7 简化部署

  • 在pom.xml中导入插件,用于将SpringBoot应用打包成一个jar包:
<build>
    <plugins>
        <!--  可以将应用打包成一个可执行的jar包   -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
  • 将这个应用打成jar包,直接使用java -jar的命令进行执行。

4 HelloWorld探究

4.1 pom文件

4.1.1 父项目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

它的父项目是

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

它是真正管理SpringBoot应用里面的所有依赖版本

可以将上面的父项目称为SpringBoot的版本仲裁中心。

所以我们导入依赖默认是不需要写版本号的,前提是版本仲裁中心里面有。

4.1.2 启动器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • spring-boot-starter:称为场景启动器。

  • spring-boot-starter-web:帮我们导入web模块正常运行所依赖的组件。

  • SpringBoot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目中引入这些starter,其相关场景的所有依赖都会导入进来。需要什么功能就导入什么场景的启动器。

4.2 主程序类(主入口类)

package com.sunxiaping.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 主程序,用来启动SpringBoot应用
 */
@SpringBootApplication
public class HelloWorldAApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldAApplication.class, args);
    }

}
  • @SpringBootApplication:SpringBoot应用标注在某个类上,说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用。
@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注解:

    • 源码如下:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Configuration
    public @interface SpringBootConfiguration {
    
    }
    
    • SpringBoot的配置类。
    • 标注在某个类上,表示这是一个Spring Boot的配置类。
    • 实际就是@Configuration注解,@Configuration注解标注在某个类上,那么这个类就是配置类,配置类也是容器中一个组件。
  • @EnableAutoConfiguration注解:

    • 源码如下:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @AutoConfigurationPackage
    @Import(EnableAutoConfigurationImportSelector.class)
    public @interface EnableAutoConfiguration {
    
    	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    
    	/**
    	 * Exclude specific auto-configuration classes such that they will never be applied.
    	 * @return the classes to exclude
    	 */
    	Class<?>[] exclude() default {};
    
    	/**
    	 * Exclude specific auto-configuration class names such that they will never be
    	 * applied.
    	 * @return the class names to exclude
    	 * @since 1.3.0
    	 */
    	String[] excludeName() default {};
    
    }
    
    • 开启自动配置功能。

    • @Import(EnableAutoConfigurationImportSelector.class):导入那些组件的选择器。

      • 导入自动配置类
      • 会给容器中导入非常多的自动配置类(xxxAutoConfiguration)。
      • 自动配置类的作用:就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
      • 有了自动配置类,就免去了我们手动编写配置注入功能组件等工作,其实就是调用了getCandidateConfigurations方法,而getCandidateConfigurations方法就是从META-INF/spring.factories中获取EnableAutoConfiguration指定的值,并将这些值作为自动配置类导入到容器中,自动配置类就生效了,就能帮助我们进行自动配置功能。

      META-INF下的spring.factories

  • @AutoConfigurationPackage注解:

    • 源码如下:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Inherited
    @Import(AutoConfigurationPackages.Registrar.class)
    public @interface AutoConfigurationPackage {
    
    }
    
    • 自动配置包。
    • @Import注解是Spring的底层注解,就是给容器中导入一个组件,@AutoConfigurationPackage导入的组件是AutoConfigurationPackages.Registrar.class。
    • 将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器中。
posted @ 2020-09-13 09:36  许大仙  阅读(180)  评论(0编辑  收藏  举报