一.SpringBoot入门
1.spring Boot简介
springboot是简化spring应用开发的一个框架;
整个spring技术栈的一个大整合;
J2EE开发的一站式解决方案;
2.微服务
2014年时martin fowler告诉我们:
微服务是一种架构风格
一个应用应该是一组小型服务;可以通过HTTP的方式进行互通;
每一个功能元素最终都是一个可以独立替换和独立升级的软件单元;
单体应用:ALL IN ONE;
详细参照微服务文档
3.环境约束
- jdk1.8:springboot建议使用jdk1.7及以上版本
- maven3.x:maven3.3以上版本
- intellijIDEA2019版本
- springboot1.5.9.release
4.Spring Boot Helloworld项目
一个功能:
浏览器发送一个hello请求,服务器接受请求并处理,响应Hello World字符串
1.创建一个maven工程(.jar)
2.导入springboot相关的依赖
<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.编写一个主程序:启动springBoot应用
编写com.test.HelloWorldMainApplication.java
/**
* @SpringBootApplication用来标注一个主程序类,说明这是一个SpringBoot应用
* */
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring应用的启动器
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4.编写相关的controller、service
编写controller.HelloController.java
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
5.在主程序当中将main方法运行起来
6.将项目进行打包,简化部署
首先在pom.xml文件中添加maven插件
<!--这个插件可以将应用打包成一个可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后点击编辑器右边的maven将该应用打包成jar包
最后生成的文件放在 \target\spring-boot-01-helloworld-1.0-SNAPSHOT.jar该文件可以直接运行:
- 首先将该文件copy出来放在桌面上C:\Users\Administrator\Desktop
- 使用cmd命令cd C:\Users\Administrator\Desktop进入当前工作页面
- 直接使用java -jar spring-boot-01-helloworld-1.0-SNAPSHOT.jar命令就可以执行该文件
5.Hello World项目探究
1.pom文件
1.父项目
我们可以看到pom文件当中有一个父项目spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
点击spring-boot-starter-parent进去发现该父项目还依赖于另一个父项目spring-boot-dependencies,
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
点进去spring-boot-dependencies就会发现它才是真正用来管理Spring Boot应用里面的所有依赖版本。
<properties>
<activemq.version>5.15.9</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.74</appengine-sdk.version>
<artemis.version>2.6.4</artemis.version>
<aspectj.version>1.9.4</aspectj.version>
<assertj.version>3.11.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.9.12</byte-buddy.version>
<caffeine.version>2.6.2</caffeine.version>
<cassandra-driver.version>3.6.0</cassandra-driver.version>
<classmate.version>1.4.0</classmate.version>
<commons-codec.version>1.11</commons-codec.version>
<commons-dbcp2.version>2.5.0</commons-dbcp2.version>
<commons-lang3.version>3.8.1</commons-lang3.version>
<commons-pool.version>1.6</commons-pool.version>
<commons-pool2.version>2.6.2</commons-pool2.version>
<couchbase-cache-client.version>2.1.0</couchbase-cache-client.version>
<couchbase-client.version>2.7.4</couchbase-client.version>
<derby.version>10.14.2.0</derby.version>
<dom4j.version>1.6.1</dom4j.version>
<dropwizard-metrics.version>4.0.5</dropwizard-metrics.version>
<ehcache.version>2.10.6</ehcache.version>
<ehcache3.version>3.6.3</ehcache3.version>
<elasticsearch.version>6.4.3</elasticsearch.version>
<embedded-mongo.version>2.1.2</embedded-mongo.version>
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<flatten-maven-plugin.version>1.0.1</flatten-maven-plugin.version>
<flyway.version>5.2.4</flyway.version>
<freemarker.version>2.3.28</freemarker.version>
<git-commit-id-plugin.version>2.2.6</git-commit-id-plugin.version>
<glassfish-el.version>3.0.0</glassfish-el.version>
<glassfish-jaxb.version>2.3.1</glassfish-jaxb.version>
<groovy.version>2.5.7</groovy.version>
<gson.version>2.8.5</gson.version>
<h2.version>1.4.199</h2.version>
<hamcrest.version>1.3</hamcrest.version>
<hazelcast.version>3.11.4</hazelcast.version>
<hazelcast-hibernate5.version>1.2.3</hazelcast-hibernate5.version>
<hibernate.version>5.3.10.Final</hibernate.version>
<hibernate-validator.version>6.0.16.Final</hibernate-validator.version>
<hikaricp.version>3.2.0</hikaricp.version>
<hsqldb.version>2.4.1</hsqldb.version>
<htmlunit.version>2.33</htmlunit.version>
<httpasyncclient.version>4.1.4</httpasyncclient.version>
<httpclient.version>4.5.8</httpclient.version>
<httpcore.version>4.4.11</httpcore.version>
<infinispan.version>9.4.13.Final</infinispan.version>
<influxdb-java.version>2.14</influxdb-java.version>
<jackson.version>2.9.8</jackson.version>
<janino.version>3.0.12</janino.version>
<javax-activation.version>1.2.0</javax-activation.version>
<javax-annotation.version>1.3.2</javax-annotation.version>
<javax-cache.version>1.1.0</javax-cache.version>
<javax-jaxb.version>2.3.1</javax-jaxb.version>
<javax-jaxws.version>2.3.1</javax-jaxws.version>
<javax-jms.version>2.0.1</javax-jms.version>
<javax-json.version>1.1.4</javax-json.version>
<javax-jsonb.version>1.0</javax-jsonb.version>
<javax-mail.version>1.6.2</javax-mail.version>
<javax-money.version>1.0.3</javax-money.version>
<javax-persistence.version>2.2</javax-persistence.version>
<javax-transaction.version>1.3</javax-transaction.version>
<javax-validation.version>2.0.1.Final</javax-validation.version>
<javax-websocket.version>1.1</javax-websocket.version>
<jaxen.version>1.1.6</jaxen.version>
<jaybird.version>3.0.5</jaybird.version>
<jboss-logging.version>3.3.2.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.3</jedis.version>
<jersey.version>2.27</jersey.version>
<jest.version>6.3.1</jest.version>
<jetty.version>9.4.18.v20190429</jetty.version>
<jetty-el.version>8.5.40</jetty-el.version>
<jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version>
<jetty-reactive-httpclient.version>1.0.3</jetty-reactive-httpclient.version>
<jmustache.version>1.14</jmustache.version>
<jna.version>4.5.2</jna.version>
<joda-time.version>2.10.2</joda-time.version>
<johnzon.version>${johnzon-jsonb.version}</johnzon.version>
<johnzon-jsonb.version>1.1.11</johnzon-jsonb.version>
<jolokia.version>1.6.1</jolokia.version>
<jooq.version>3.11.11</jooq.version>
<jsonassert.version>1.5.0</jsonassert.version>
<json-path.version>2.4.0</json-path.version>
<jstl.version>1.2</jstl.version>
<jtds.version>1.3.1</jtds.version>
<junit.version>4.12</junit.version>
<junit-jupiter.version>5.3.2</junit-jupiter.version>
<kafka.version>2.0.1</kafka.version>
<kotlin.version>1.2.71</kotlin.version>
<lettuce.version>5.1.6.RELEASE</lettuce.version>
<liquibase.version>3.6.3</liquibase.version>
<log4j2.version>2.11.2</log4j2.version>
<logback.version>1.2.3</logback.version>
<lombok.version>1.18.8</lombok.version>
<mariadb.version>2.3.0</mariadb.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<maven-assembly-plugin.version>3.1.1</maven-assembly-plugin.version>
<maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-enforcer-plugin.version>3.0.0-M2</maven-enforcer-plugin.version>
<maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version>
<maven-help-plugin.version>3.1.1</maven-help-plugin.version>
<maven-install-plugin.version>2.5.2</maven-install-plugin.version>
<maven-invoker-plugin.version>3.1.0</maven-invoker-plugin.version>
<maven-jar-plugin.version>3.1.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.0.1</maven-javadoc-plugin.version>
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
<maven-site-plugin.version>3.7.1</maven-site-plugin.version>
<maven-source-plugin.version>3.0.1</maven-source-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<maven-war-plugin.version>3.2.2</maven-war-plugin.version>
<micrometer.version>1.1.4</micrometer.version>
<mimepull.version>1.9.11</mimepull.version>
<mockito.version>2.23.4</mockito.version>
<mongodb.version>3.8.2</mongodb.version>
<mongo-driver-reactivestreams.version>1.9.2</mongo-driver-reactivestreams.version>
<mssql-jdbc.version>6.4.0.jre8</mssql-jdbc.version>
<mysql.version>8.0.16</mysql.version>
<nekohtml.version>1.9.22</nekohtml.version>
<neo4j-ogm.version>3.1.9</neo4j-ogm.version>
<netty.version>4.1.36.Final</netty.version>
<netty-tcnative.version>2.0.25.Final</netty-tcnative.version>
<nio-multipart-parser.version>1.1.0</nio-multipart-parser.version>
<pooled-jms.version>1.0.4</pooled-jms.version>
<postgresql.version>42.2.5</postgresql.version>
<prometheus-pushgateway.version>0.5.0</prometheus-pushgateway.version>
<quartz.version>2.3.1</quartz.version>
<querydsl.version>4.2.1</querydsl.version>
<rabbit-amqp-client.version>5.4.3</rabbit-amqp-client.version>
<reactive-streams.version>1.0.2</reactive-streams.version>
<reactor-bom.version>Californium-SR8</reactor-bom.version>
<rest-assured.version>3.1.1</rest-assured.version>
<rxjava.version>1.3.8</rxjava.version>
<rxjava2.version>2.2.8</rxjava2.version>
<rxjava-adapter.version>1.2.1</rxjava-adapter.version>
<saaj-impl.version>1.5.0</saaj-impl.version>
<selenium.version>3.14.0</selenium.version>
<selenium-htmlunit.version>2.33.0</selenium-htmlunit.version>
<sendgrid.version>4.3.0</sendgrid.version>
<servlet-api.version>4.0.1</servlet-api.version>
<slf4j.version>1.7.26</slf4j.version>
<snakeyaml.version>1.23</snakeyaml.version>
<solr.version>7.7.1</solr.version>
<spring.version>5.1.7.RELEASE</spring.version>
<spring-amqp.version>2.1.6.RELEASE</spring-amqp.version>
<spring-batch.version>4.1.2.RELEASE</spring-batch.version>
<spring-cloud-connectors.version>2.0.5.RELEASE</spring-cloud-connectors.version>
<spring-data-releasetrain.version>Lovelace-SR8</spring-data-releasetrain.version>
<spring-framework.version>${spring.version}</spring-framework.version>
<spring-hateoas.version>0.25.1.RELEASE</spring-hateoas.version>
<spring-integration.version>5.1.5.RELEASE</spring-integration.version>
<spring-kafka.version>2.2.6.RELEASE</spring-kafka.version>
<spring-ldap.version>2.3.2.RELEASE</spring-ldap.version>
<spring-plugin.version>1.2.0.RELEASE</spring-plugin.version>
<spring-restdocs.version>2.0.3.RELEASE</spring-restdocs.version>
<spring-retry.version>1.2.4.RELEASE</spring-retry.version>
<spring-security.version>5.1.5.RELEASE</spring-security.version>
<spring-session-bom.version>Bean-SR5</spring-session-bom.version>
<spring-ws.version>3.0.7.RELEASE</spring-ws.version>
<sqlite-jdbc.version>3.25.2</sqlite-jdbc.version>
<statsd-client.version>3.1.0</statsd-client.version>
<sun-mail.version>${javax-mail.version}</sun-mail.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
<thymeleaf-extras-java8time.version>3.0.4.RELEASE</thymeleaf-extras-java8time.version>
<thymeleaf-extras-springsecurity.version>3.0.4.RELEASE</thymeleaf-extras-springsecurity.version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
<tomcat.version>9.0.19</tomcat.version>
<unboundid-ldapsdk.version>4.0.10</unboundid-ldapsdk.version>
<undertow.version>2.0.20.Final</undertow.version>
<versions-maven-plugin.version>2.7</versions-maven-plugin.version>
<webjars-hal-browser.version>3325375</webjars-hal-browser.version>
<webjars-locator-core.version>0.35</webjars-locator-core.version>
<wsdl4j.version>1.6.3</wsdl4j.version>
<xml-apis.version>1.4.01</xml-apis.version>
<xml-maven-plugin.version>1.0.2</xml-maven-plugin.version>
<xmlunit2.version>2.6.2</xmlunit2.version>
</properties>
可以把它理解为Spring Boot的版本仲裁中心。以后我们导入依赖默认是不需要写版本号的。只有那些没有在spring-boot-dependencies里面管理的依赖才需要写版本号。
2.启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web:
spring-boot-starter:springboot大的场景启动器:帮我们导入了web模块正常运行所依赖的组件;
SpringBoot将所有的功能场景都抽取出来了,做成了一个个的starters(启动器),只需要在项目里面引入这些starters相关场景的所有依赖都会导入进来,需要用到什么功能就导入什么场景的启动器
2.主程序类、主入口类
/**
* @SpringBootApplication用来标注一个主程序类,说明这是一个SpringBoot应用
* */
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring应用的启动器
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
我们首先来看一看@SpringBootApplication这个注解:
@SpringBootApplication:Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动springBoot应用;
点进去SpringBootApplication 就会发现这是一个组合注解
@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注解:
@SpringBootConfiguration:SpringBoot配置类:标注在某个类上表示这是一个SpringBoot的配置类,点进去这个类就会发现:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
@Configuration:Configuration是spring底层定义的注解,配置类上来标注这个注解;
注意:配置类==配置文件,配置类也是容器中的一个组件;@Component
再来看看@EnableAutoConfiguration这个注解:
@EnableAutoConfiguration:开启自动配置功能;以前我们需要配置的东西现在都由SpringBoot帮我们自动配置,而@EnableAutoConfiguration这个注解就告诉了SpringBoot开启自动配置功能;这样自动配置才能生效;
点开@EnableAutoConfiguration这个注解我们可以看到:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {}
在@EnableAutoConfiguration这个注解当中存在一个@AutoConfigurationPackage注解:
@AutoConfigurationPackage:自动配置包;该注解将主配置类(@SpringBootApplication标注的类)的所在包以及下面所有子包里面的所有组件都扫描到Spring容器。点进这个注解可以看到:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
@Import({Registrar.class}):这个注解是Spring的底层注解;作用是给容器中导入一个组件;导入的组件由Registrar.class这个类来决定。
在@EnableAutoConfiguration注解当中还存在一个注解@Import({AutoConfigurationImportSelector.class}):给容器中导入AutoConfigurationImportSelector这个组件。
AutoConfigurationImportSelector:导入哪些选择器,将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器当中。会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件,并配置好这些组件;有了自动配置类就免去了手动编写配置和注入功能组件等工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效了,帮我们进行自动配置工作。
以前我们需要自己配置的东西,自动配置类都帮我们配置好了。
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar;
6.使用Spring Initializer快速创建SpringBoot项目
IDE都支持使用spring的项目创建向导快速创建一个项目;
选择我们需要的模块;向导会联网创建SpringBoot项目;
默认生成的SpringBoot项目的特点:
- 主程序已经生成好了,我们只需要写我们自己的逻辑
- resources文件夹中的目录结构:
- static:保存所有的静态资源:js css images;
- templates:保存所有的模板页面;(SpringBoot默认jar包使用嵌入式的Tomcat,不支持JSP页面),可以使用模板引擎(freemarker,thymeleaf);
- application.properties:SpringBoot应用的配置文件,可以修改一些默认设置