• 1. Spring Boot入门


    1.Spring Boot简介

      简化Spring应用开发的一个框架

      整个Spring技术栈的一个大整合

      J2EE开发的一站式解决方案

    优点:

    – 快速创建独立运行的Spring项目以及与主流框架集成

    – 使用嵌入式的Servlet容器,应用无需打成WAR包 – starters自动依赖与版本控制

    – 大量的自动配置,简化开发,也可修改默认值 – 无需配置XML,无代码生成,开箱即用

    – 准生产环境的运行时应用监控

    – 与云计算的天然集成

    2.微服务

      提出者:martin fowler

    微服务:架构风格(服务微化)

      一个应用应该是一组小型服务;可以通过HTTP的方式进行互通;

    单体应用:ALL IN ONE

      微服务:每一个功能元素最终都是一个可独立替换和独立升级的软件单元;

      微服务文档:https://martinfowler.com/articles/microservices.html#MicroservicesAndSoa

    3.环境准备

    [1].JDK1.8

      Spring Boot 推荐jdk1.7及以上;

      java version "1.8.0_201"

    C:WINDOWSsystem32>java -version

    java version "1.8.0_201"

    Java(TM) SE Runtime Environment (build 1.8.0_201-b09)

    Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

    [2].Maven3.x

      maven 3.3以上版本;

      Apache Maven 3.3.9

    C:WINDOWSsystem32>mvn -v

    Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)

    Maven home: D:DevInstallapache-maven-3.3.9

    Java version: 1.8.0_201, vendor: Oracle Corporation

    Java home: D:Program FilesJavajdk1.8.0_201jre

    Default locale: zh_CN, platform encoding: GBK

    OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"

      Maven设置:给Maven的settings.xml配置文件的profiles标签添加

        </profile>

        -->

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

      </profiles>

    [3].IDEA2017

      IntelliJ IDEA 2017.3.7 x64

      STS

    [4].Spring Boot

      SpringBoot 1.5.9.RELEASE:1.5.9;

     

    IDEA设置

    (1).整合Maven

      Configure->settings

      

    4.Spring Boot HelloWorld

      浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串;

    [1].创建Maven工程

      SDK选择我们安装的JDK位置即可

      

    [2].导入依赖spring Boot相关的依赖

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

    [3].编写主程序main

    /**

     *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用

     */

    @SpringBootApplication

    public class HelloWorldMainApplication {

        public static void main(String[] args) {

            // Spring应用启动起来

            SpringApplication.run(HelloWorldMainApplication.class,args);

        }

    }

    [4].编写Controller、Service

    @Controller

    public class HelloController {

        @ResponseBody

        @RequestMapping("/hello")

        public String hello(){

            return "Hello World!";

        }

    }

    [5].运行主程序测试

    [6].简化部署

      将这个应用打成jar包,直接使用java -jar的命令进行执行;

        <!-- 该插件可以将应用打包成可执行的jar-->

        <build>

            <plugins>

                <plugin>

                    <groupId>org.springframework.boot</groupId>

                    <artifactId>spring-boot-maven-plugin</artifactId>

                </plugin>

            </plugins>

        </build>

    5.HelloWorld解析

    [1].POM文件

    (1).父项目

        <parent>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-parent</artifactId>

            <version>1.5.9.RELEASE</version>

        </parent>

    它的父项目

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-dependencies</artifactId>

    <version>1.5.9.RELEASE</version>

    <relativePath>../../spring-boot-dependencies</relativePath>

    </parent>

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

    <properties>

    <!-- Dependency versions -->

    <activemq.version>5.14.5</activemq.version>

    <antlr2.version>2.7.7</antlr2.version>

    <appengine-sdk.version>1.9.59</appengine-sdk.version>

    <artemis.version>1.5.5</artemis.version>

    <aspectj.version>1.8.13</aspectj.version>

    <assertj.version>2.6.0</assertj.version>

    <atomikos.version>3.9.3</atomikos.version>

    <bitronix.version>2.1.4</bitronix.version>

    <caffeine.version>2.3.5</caffeine.version>

    <cassandra-driver.version>3.1.4</cassandra-driver.version>

    <classmate.version>1.3.4</classmate.version>

    <commons-beanutils.version>1.9.3</commons-beanutils.version>

    <commons-collections.version>3.2.2</commons-collections.version>

    <commons-codec.version>1.10</commons-codec.version>

    <commons-dbcp.version>1.4</commons-dbcp.version>

    <commons-dbcp2.version>2.1.1</commons-dbcp2.version>

    <commons-digester.version>2.1</commons-digester.version>

    <commons-pool.version>1.6</commons-pool.version>

    <commons-pool2.version>2.4.3</commons-pool2.version>

    <couchbase-client.version>2.3.7</couchbase-client.version>

    <couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>

    <crashub.version>1.3.2</crashub.version>

    <derby.version>10.13.1.1</derby.version>

    <dom4j.version>1.6.1</dom4j.version>

    <dropwizard-metrics.version>3.1.5</dropwizard-metrics.version>

    <ehcache.version>2.10.4</ehcache.version>

    <ehcache3.version>3.2.3</ehcache3.version>

    <embedded-mongo.version>1.50.5</embedded-mongo.version>

    <flyway.version>3.2.1</flyway.version>

    <freemarker.version>2.3.27-incubating</freemarker.version>

    <elasticsearch.version>2.4.6</elasticsearch.version>

    <gemfire.version>8.2.7</gemfire.version>

    <glassfish-el.version>3.0.0</glassfish-el.version>

    <gradle.version>2.9</gradle.version>

    <groovy.version>2.4.13</groovy.version>

    <gson.version>2.8.2</gson.version>

    <h2.version>1.4.196</h2.version>

    <hamcrest.version>1.3</hamcrest.version>

    <hazelcast.version>3.7.8</hazelcast.version>

    <hazelcast-hibernate4.version>3.7.1</hazelcast-hibernate4.version>

    <hazelcast-hibernate5.version>1.1.3</hazelcast-hibernate5.version>

    <hibernate.version>5.0.12.Final</hibernate.version>

    <hibernate-validator.version>5.3.6.Final</hibernate-validator.version>

    <hikaricp.version>2.5.1</hikaricp.version>

    <hikaricp-java6.version>2.3.13</hikaricp-java6.version>

    <hikaricp-java7.version>2.4.13</hikaricp-java7.version>

    <hsqldb.version>2.3.5</hsqldb.version>

    <htmlunit.version>2.21</htmlunit.version>

    <httpasyncclient.version>4.1.3</httpasyncclient.version>

    <httpclient.version>4.5.3</httpclient.version>

    <httpcore.version>4.4.8</httpcore.version>

    <infinispan.version>8.2.8.Final</infinispan.version>

    <jackson.version>2.8.10</jackson.version>

    <janino.version>2.7.8</janino.version>

    <javassist.version>3.21.0-GA</javassist.version> <!-- Same as Hibernate -->

    <javax-cache.version>1.0.0</javax-cache.version>

    <javax-mail.version>1.5.6</javax-mail.version>

    <javax-transaction.version>1.2</javax-transaction.version>

    <javax-validation.version>1.1.0.Final</javax-validation.version>

    <jaxen.version>1.1.6</jaxen.version>

    <jaybird.version>2.2.13</jaybird.version>

    <jboss-logging.version>3.3.1.Final</jboss-logging.version>

    <jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version>

    <jdom2.version>2.0.6</jdom2.version>

    <jedis.version>2.9.0</jedis.version>

    <jersey.version>2.25.1</jersey.version>

    <jest.version>2.0.4</jest.version>

    <jetty.version>9.4.7.v20170914</jetty.version>

    <jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>

    <jetty-el.version>8.0.33</jetty-el.version>

    <jms-api.version>1.1-rev-1</jms-api.version>

    <jmustache.version>1.13</jmustache.version>

    <jna.version>4.2.2</jna.version>

    <joda-time.version>2.9.9</joda-time.version>

    <jolokia.version>1.3.7</jolokia.version>

    <jooq.version>3.9.6</jooq.version>

    <json.version>20140107</json.version>

    <jsonassert.version>1.4.0</jsonassert.version>

    <json-path.version>2.2.0</json-path.version>

    <jstl.version>1.2</jstl.version>

    <jtds.version>1.3.1</jtds.version>

    <junit.version>4.12</junit.version>

    <liquibase.version>3.5.3</liquibase.version>

    <log4j2.version>2.7</log4j2.version>

    <logback.version>1.1.11</logback.version>

    <lombok.version>1.16.18</lombok.version>

    <mariadb.version>1.5.9</mariadb.version>

    <mssql-jdbc.version>6.1.0.jre7</mssql-jdbc.version>

    <mockito.version>1.10.19</mockito.version>

    <mongodb.version>3.4.3</mongodb.version>

    <mysql.version>5.1.44</mysql.version>

    <narayana.version>5.5.30.Final</narayana.version>

    <nekohtml.version>1.9.22</nekohtml.version>

    <neo4j-ogm.version>2.1.5</neo4j-ogm.version>

    <postgresql.version>9.4.1212.jre7</postgresql.version>

    <querydsl.version>4.1.4</querydsl.version>

    <reactor.version>2.0.8.RELEASE</reactor.version>

    <reactor-spring.version>2.0.7.RELEASE</reactor-spring.version>

    <selenium.version>2.53.1</selenium.version>

    <selenium-htmlunit.version>2.21</selenium-htmlunit.version>

    <sendgrid.version>2.2.2</sendgrid.version>

    <servlet-api.version>3.1.0</servlet-api.version>

    <simple-json.version>1.1.1</simple-json.version>

    <slf4j.version>1.7.25</slf4j.version>

    <snakeyaml.version>1.17</snakeyaml.version>

    <solr.version>5.5.5</solr.version>

    <spock.version>1.0-groovy-2.4</spock.version>

    <spring.version>4.3.13.RELEASE</spring.version>

    <spring-amqp.version>1.7.4.RELEASE</spring-amqp.version>

    <spring-cloud-connectors.version>1.2.5.RELEASE</spring-cloud-connectors.version>

    <spring-batch.version>3.0.8.RELEASE</spring-batch.version>

    <spring-data-releasetrain.version>Ingalls-SR9</spring-data-releasetrain.version>

    <spring-hateoas.version>0.23.0.RELEASE</spring-hateoas.version>

    <spring-integration.version>4.3.12.RELEASE</spring-integration.version>

    <spring-integration-java-dsl.version>1.2.3.RELEASE</spring-integration-java-dsl.version>

    <spring-kafka.version>1.1.7.RELEASE</spring-kafka.version>

    <spring-ldap.version>2.3.2.RELEASE</spring-ldap.version>

    <spring-loaded.version>1.2.8.RELEASE</spring-loaded.version>

    <spring-mobile.version>1.1.5.RELEASE</spring-mobile.version>

    <spring-plugin.version>1.2.0.RELEASE</spring-plugin.version>

    <spring-restdocs.version>1.1.3.RELEASE</spring-restdocs.version>

    <spring-retry.version>1.2.1.RELEASE</spring-retry.version>

    <spring-security.version>4.2.3.RELEASE</spring-security.version>

    <spring-security-jwt.version>1.0.8.RELEASE</spring-security-jwt.version>

    <spring-security-oauth.version>2.0.14.RELEASE</spring-security-oauth.version>

    <spring-session.version>1.3.1.RELEASE</spring-session.version>

    <spring-social.version>1.1.4.RELEASE</spring-social.version>

    <spring-social-facebook.version>2.0.3.RELEASE</spring-social-facebook.version>

    <spring-social-linkedin.version>1.0.2.RELEASE</spring-social-linkedin.version>

    <spring-social-twitter.version>1.1.2.RELEASE</spring-social-twitter.version>

    <spring-ws.version>2.4.2.RELEASE</spring-ws.version>

    <sqlite-jdbc.version>3.15.1</sqlite-jdbc.version>

    <statsd-client.version>3.1.0</statsd-client.version>

    <sun-mail.version>${javax-mail.version}</sun-mail.version>

    <thymeleaf.version>2.1.6.RELEASE</thymeleaf.version>

    <thymeleaf-extras-springsecurity4.version>2.1.3.RELEASE</thymeleaf-extras-springsecurity4.version>

    <thymeleaf-extras-conditionalcomments.version>2.1.2.RELEASE</thymeleaf-extras-conditionalcomments.version>

    <thymeleaf-layout-dialect.version>1.4.0</thymeleaf-layout-dialect.version>

    <thymeleaf-extras-data-attribute.version>1.3</thymeleaf-extras-data-attribute.version>

    <thymeleaf-extras-java8time.version>2.1.0.RELEASE</thymeleaf-extras-java8time.version>

    <tomcat.version>8.5.23</tomcat.version>

    <undertow.version>1.4.21.Final</undertow.version>

    <unboundid-ldapsdk.version>3.2.1</unboundid-ldapsdk.version>

    <webjars-hal-browser.version>9f96c74</webjars-hal-browser.version>

    <webjars-locator.version>0.32-1</webjars-locator.version>

    <wsdl4j.version>1.6.3</wsdl4j.version>

    <xml-apis.version>1.4.01</xml-apis.version>

    <!-- Plugin versions -->

    <build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>

    <exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>

    <git-commit-id-plugin.version>2.2.3</git-commit-id-plugin.version>

    <maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>

    <maven-assembly-plugin.version>2.6</maven-assembly-plugin.version>

    <maven-clean-plugin.version>2.6.1</maven-clean-plugin.version>

    <maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>

    <maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>

    <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>

    <maven-eclipse-plugin.version>2.10</maven-eclipse-plugin.version>

    <maven-enforcer-plugin.version>1.4</maven-enforcer-plugin.version>

    <maven-failsafe-plugin.version>2.18.1</maven-failsafe-plugin.version>

    <maven-install-plugin.version>2.5.2</maven-install-plugin.version>

    <maven-invoker-plugin.version>1.10</maven-invoker-plugin.version>

    <maven-help-plugin.version>2.2</maven-help-plugin.version>

    <maven-jar-plugin.version>2.6</maven-jar-plugin.version>

    <maven-javadoc-plugin.version>2.10.4</maven-javadoc-plugin.version>

    <maven-resources-plugin.version>2.7</maven-resources-plugin.version>

    <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>

    <maven-site-plugin.version>3.5.1</maven-site-plugin.version>

    <maven-source-plugin.version>2.4</maven-source-plugin.version>

    <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>

    <maven-war-plugin.version>2.6</maven-war-plugin.version>

    <versions-maven-plugin.version>2.2</versions-maven-plugin.version>

    </properties>

      Spring Boot的版本中心;

      以后我们导入依赖默认是不需要写版本;(没有在dependencies里面管理的依赖自然需要声明版本号)

    (2).启动器

        <dependencies>

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-web</artifactId>

            </dependency>

        </dependencies>

      spring-boot-starter-webSpring-boot场景启动器,帮我们导入了web模块正常运行所依赖的组件.

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-tomcat</artifactId>

    </dependency>

    <dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-validator</artifactId>

    </dependency>

    <dependency>

    <groupId>com.fasterxml.jackson.core</groupId>

    <artifactId>jackson-databind</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-webmvc</artifactId>

    </dependency>

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

    [2].主程序类(主入口类)

    /**

     * @SpringBootApplication 来标注一个主程序类 说明这是一个Spring boot应用

     */

    @SpringBootApplication

    public class HelloWorldMainApplication {

        public static void main(String[] args) {

            //Spring应用启动起来

            SpringApplication.run(HelloWorldMainApplication.class,args);

        }

    }

    @SpringBootApplication: Spring Boot应用标注在某个类上说明这个类是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:Spring Boot的配置类;

      标注在某个类上,表示这是一个Spring Boot的配置类

        |- @Configuration:配置类上来标注这个注解;

          配置类 -----  配置文件;

       配置类也是容器中的一个组件;

      |- @Component

    @EnableAutoConfiguration:开启自动配置功能;

      以前我们需要配置的东西,Spring Boot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效;

    @AutoConfigurationPackage

    @Import(EnableAutoConfigurationImportSelector.class)

    public @interface EnableAutoConfiguration {}

    @AutoConfigurationPackage:自动配置包;

      |- @Import(AutoConfigurationPackages.Registrar.class)

        Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class;

     将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;

            public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {

                AutoConfigurationPackages.register(registry, (new AutoConfigurationPackages.PackageImport(metadata)).getPackageName());

            }

    @Import({EnableAutoConfigurationImportSelector.class}):给容器中导入组件;

      |- EnableAutoConfigurationImportSelector:导入那些组件的选择器

        将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;

        会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;

        public String[] selectImports(AnnotationMetadata annotationMetadata) {

            if (!this.isEnabled(annotationMetadata)) {

                return NO_IMPORTS;

            } else {

                try {

                    AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);

                    AnnotationAttributes attributes = this.getAttributes(annotationMetadata);

                    List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);

                    configurations = this.removeDuplicates(configurations);

                    configurations = this.sort(configurations, autoConfigurationMetadata);

                    Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);

                    this.checkExcludedClasses(configurations, exclusions);

                    configurations.removeAll(exclusions);

                    configurations = this.filter(configurations, autoConfigurationMetadata);

                    this.fireAutoConfigurationImportEvents(configurations, exclusions);

                    return (String[])configurations.toArray(new String[configurations.size()]);

                } catch (IOException var6) {

                    throw new IllegalStateException(var6);

                }

            }

        }

     

      有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;     

    SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);

      Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东西,自动配置类都帮我们;

      J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;

     

    6.使用Spring Initializer快速创建Spring Boot项目

    [1].IDEA:使用 Spring Initializer快速创建项目

     

    IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目;

    选择我们需要的模块;向导会联网创建Spring Boot项目;

     

    默认生成的Spring Boot项目;

    - 主程序已经生成好了,我们只需要我们自己的逻辑

    - resources文件夹中目录结构

      - static:保存所有的静态资源; js css  images;

      - templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);

      - application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;

    [2].STS使用Spring Starter Project快速创建项目

    参考文档:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/

  • 相关阅读:
    append 注意事项
    列表与字符串转换
    删除列表元素
    段寄存器
    通用寄存器
    最强大的王爽汇编语言学习环境使用教程
    JavaScript获取输入框内容
    ubuntu16.04中将python3设置为默认
    Django之cookie和session
    postman 安装桌面版
  • 原文地址:https://www.cnblogs.com/HOsystem/p/13779865.html
Copyright © 2020-2023  润新知