自定义FailureAnalyzer
Step1:自定义异常
|
|
Step2:自定义FailureAnalyzer
|
|
Step3:配置FailureAnalyzer
|
|
Step4:定义一个Service被Spring初始化抛出该异常
|
|
Step5:构建一个Spring Application
|
|
Step6:运行Spring Application
|
|
|
|
|
|
|
|
|
|
|
|
|
|
本文主要介绍spring-boot
官方提供的常用的starters
,对应的spring-boot
版本均为1.4.2.RELEASE
,官方文档地址为http://docs.spring.io//spring-boot/docs/1.4.2.RELEASE/reference/html/using-boot-build-systems.html#using-boot-starter,官方提供的starters
均以spring-boot-starter-
开头,第三方提供的starters
均以acme-spring-boot-starter
开头,当然你也可以自己定义starters
。
Name | Description | Pom |
---|---|---|
spring-boot-starter-data-redis |
集成了Spring Data Redis 和redis,redis 是一种NoSQL 数据库 |
Pom |
spring-boot-starter-data-mongodb |
集成了Spring Data MongoDB 和mongodb,mongodb 是一种NoSQL 数据库 |
Pom |
spring-boot-starter-data-neo4j |
集成了Spring Data Neo4j 和Neo4j,Neo4j 是一种图数据库 |
Pom |
spring-boot-starter-data-couchbase |
集成了Spring Data Couchbase 和Couchbase,couchbase 是一种NoSQL 数据库 |
Pom |
spring-boot-starter-data-cassandra |
集成了Spring Data Cassandra 和cassandra,cassandra 是一种开源的分布式NOSQL 数据库,最早由facebook 开发,后捐赠给Apache |
Pom |
spring-boot-starter-data-gemfire |
集成了Spring Data GemFire 和GemFire,GemFire 是一种NoSQL 数据库 |
Pom |
这篇文章解释了什么是一个Servlet Container
。目前Servlet Container
有很多,详细可以参考维基百科。spring-boot
集成了以下几款开源的Servlet Container
:
tomcat
:tomcat
是spring-boot
默认集成的Servlet Container
。undertow
:undertow
是一款轻量级、灵活的、可嵌入的、并支持Web Socket
协议的开源容器。jetty
:jetty
是一款由eclipse
维护的开源容器,目前使用也很广泛。Name | Description | Pom |
---|---|---|
spring-boot-starter-tomcat |
是spring-boot-starter-web 默认集成的容器 |
Pom |
spring-boot-starter-undertow |
集成了undertow |
Pom |
spring-boot-starter-jetty |
集成了jetty |
Pom |
Rest
是representational state transfer
的缩写,意思是表达性状态转换。Rest
是一种架构风格,它包含了一个分布式超文本系统中对于组件、连接器和数据的约束。想了解更多关于Rest点击此处HATEOAS
是Hypermedia as the engine of application state
的缩写,意思是超媒体即应用状态引擎。HATEOAS
是Rest
架构风格中最复杂的约束,也是构建成熟Rest
服务的核心。
JAX-RS
是Java API for RESTful Web Services
的缩写,是一个Java编程语言的应用程序接口,支持按照Rest
架构风格创建Web服务。目前已知的JAX-RS
有4种,Jersey
是其中的一种实现,由Sun
开发的产品级质量的JAX-RS
的参考实现。当然,Jersey
也支持HATEOAS
约束。
spring-hateoas
和spring-data-rest
是spring
旗下的两个子项目,spring-data-rest
包含了spring-hateoas
,虽然spring-hateoas
实现了HATEOAS
,但是spring-data-rest
并没有真正的实现JAX-RS
规范。
spring-boot
提供了以下几种来快速构建Rest
服务:
Name | Description | Pom |
---|---|---|
spring-boot-starter-web |
默认是支持RESTful 的 |
Pom |
spring-boot-starter-hateoas |
集成了spring-mvc 和spring-hateoas |
Pom |
spring-boot-starter-jersey |
集成了spring-mvc 和Jersey |
Pom |
spring-boot-starter-data-rest |
集成了spring-data-rest 和spring-mvc |
Pom |
spring-social
是spring
旗下的一个扩展子项目,
它主要方便你对Facebook
,Twitter
,LinkedIn
等SAAS服务提供的API的一个使用。目前spring-social
的最新版本是1.1.4.RELEASE
。它有以下几个模块组成:
spring-social-web
spring-social-config
spring-social-core
spring-social-security
目前已正式发布支持的有Facebook
,Twitter
,LinkedIn
spring-social-linkedin
spring-social-facebook
spring-social-twitter
正在孵化的有Github
和Tripit
spring-social-github
spring-social-tripit
spring-boot
集成了Facebook
,Twitter
,LinkedIn
Name | Description | Pom |
---|---|---|
spring-boot-starter-social-linkedin |
集成了spring-boot-starter-web 和spring-social-linkedin |
Pom |
spring-boot-starter-social-facebook |
集成了spring-boot-starter-web 和spring-social-facebook |
Pom |
spring-boot-starter-social-twitter |
集成了spring-boot-starter-web 和spring-social-twitter |
Pom |
JTA
是Java Transaction API
的缩写,意思是Java事务API
,是一个Java企业版的应用程序接口,在Java环境中,允许完成跨越多个XA资源的分布式事务。JTA是在Java社区过程下制定的规范。
目前开源的JTA
实现有以下几个:
narayana
:narayana
是jboss
下的一款开源的JTA
bitronix
:bitronix
是一款开源的JTA
,不过目前已没人维护atomikos
:atomikos
目前提供了商业版本的维护,但其社区版仍然是开源的。Name | Description | Pom |
---|---|---|
spring-boot-starter-jta-narayana |
集成了JTA transactions 和narayana |
Pom |
spring-boot-starter-jta-bitronix |
集成了JTA transactions 和bitronix |
Pom |
spring-boot-starter-jta-atomikos |
集成了JTA transactions 和atomikos |
Pom |
spring-boot-1.4.2.RELEASE
内置支持的模板引擎有thymeleaf
,mustache
,groovy
,freemarker
,velocity
。不过从1.4
将不再支持velocity
。
Name | Description | Pom |
---|---|---|
spring-boot-starter-thymeleaf |
集成了spring-boot-starter-web 和thymeleaf 模板引擎 |
Pom |
spring-boot-starter-mustache |
集成了spring-boot-starter-web 和mustache 模板引擎 |
Pom |
spring-boot-starter-groovy-templates |
集成了spring-boot-starter-web 和groovy-templates 模板引擎 |
Pom |
spring-boot-starter-freemarker |
集成了spring-boot-starter-web 和freemarker 模板引擎 |
Pom |
spring-boot-starter-velocity |
集成了spring-boot-starter-web 和velocity 模板引擎 |
Pom |
目前主流的Java日志框架有log4j
,logback
和log4j2
。spring-boot
集成了logback
和log4j2
。
Name | Description | Pom |
---|---|---|
spring-boot-starter-logging |
集成logback |
Pom |
spring-boot-starter-log4j2 |
集成log4j2 |
Pom |
目前主流的Java测试框架有JUnit
,Hamcrest
和Mockito
。spring-boot
集成了这三者。
Name | Description | Pom |
---|---|---|
spring-boot-starter-test |
集成了JUnit ,Hamcrest 和Mockito |
Pom |
spring-security
是spring
旗下的一个子项目,支持授权和认证,可以防止session fixation
(会话固定)、clickjacking
(点击劫持)、cross site request forgery(CSRF)(跨站请求伪造)等攻击。spring-boot
集成了spring-security
。
Name | Description | Pom |
---|---|---|
spring-boot-starter-security |
集成了spring-security |
Pom |
JMS
是Java Message Service
的缩写,意思是Java消息服务,是一个Java服务中关于面向消息中间件的API,用于在两个应用程序之间或分布式系统中发送消息,进行异步通信。JMS
规范包括两种消息模式,点对点和发布者/订阅者,支持同步和异步的消息处理,支持面向事件的方法接收消息。目前主流的开源实现有Apache ActiveMQ
和Kafka
。
AMQP
是Advanced Message Queuing Protocol
的缩写,意思是高级消息队列协议,是一个异步消息传递所使用的应用层协议规范。作为线路层协议,而不是API(例如JMS
),AMQP客户端能够无视消息的来源任意发送和接受信息。目前主流的开源实现有RabbitMQ
。
JMS
和AMQP
的对比:
JMS | AMQP | |
---|---|---|
定义 | JAVA API | Protocol |
跨语言 | 否 | 是 |
跨平台 | 否 | 是 |
消息模型 | one to one and publish/subscriber | direct, fanout, topic, headers |
消息类型 | text,map,byte,stream,object | byte |
spring-boot
集成了ActiveMQ
和RabbitMQ
。
Name | Description | Pom |
---|---|---|
spring-boot-starter-activemq |
集成ActiveMQ |
Pom |
spring-boot-starter-amqp |
集成RabbitMQ |
Pom |
Name | Description | Pom |
---|---|---|
本文主要介绍spring-boot
的SpringApplication
特性,对应的spring-boot
版本均为1.4.2.RELEASE
,官方文档地址为http://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/boot-features-spring-application.html
SpringApplication
提供了一种最简单的方式来启动应用,那就是通过main
函数,只需要一行代码就可以启动应用。
|
|
运行main
函数,会输出如下信息
|
|
这里省略了一些信息的输出,从这些输出的信息可以看到
Apache Tomcat/8.5.6
的Tomcat8080
INFO
PID
)为46716Spring Boot
从1.4
版本之后新增了FailureAnalyzer
接口,它可以拦截异常,并将其转换为更易读的诊断信息。比如当Spring Application
试图使用已被占用的端口时,将显示如下信息
|
|
Spring Boot-1.4.2-RELEASE
版本内置了以下几个FailureAnalyzer
BeanCurrentlyInCreationFailureAnalyzer
BeanNotOfRequiredTypeFailureAnalyzer
BindFailureAnalyzer
ConnectorStartFailureAnalyzer
NoUniqueBeanDefinitionFailureAnalyzer
PortInUseFailureAnalyzer
ValidationExceptionFailureAnalyzer
你可以从spring-boot-1.4.2-RELEASE.jar
里的META-INF/spring.factories
找到上述几个FailureAnalyzer
Spring Application
默认是开启FailureAnalyzer
的,并没有提供关闭的配置FailureAnalyzer
的配置只能通过配置文件注入FailureAnalyzer
,你仍然可以通过--debug
来打印诊断信息,比如java -jar myproject-0.0.1-SNAPSHOT.jar --debug
如果你不喜欢默认的Spring Application
,你当然可以自己去定义一个Spring Application
,比如关闭Banner
。
|
|
Spring Boot
也提供了基于配置文件application.properties
的形式来构建一个Spring Application
。具体的用法可以参考Externalized Configuration
Spring Boot
提供了SpringApplicationBuilder
以流式的形式来构建Spring Application
。
|
|
除了通常的Spring框架的事件,比如ContextRefreshedEvent
,一个SpringApplication
还会发送一些额外的应用事件。
你不能使用
@Bean
去注册一个监听器去监听在ApplicationContext
被创建之前触发的那些事件上。你可以通过SpringApplication.addListeners(…)
或者SpringApplicationBuilder.listeners(…)
来注册。
如果你想自动注册这些监听器不管应用以何种方式启动,你可以在你的工程下创建一个文件META-INF/spring.factories
,然后引用以下配置
1 org.springframework.context.ApplicationListener=com.example.project.MyListener
当你的应用程序运行时,应用事件按下列顺序发送:
ApplicationStartedEvent
:启动时发送该事件ApplicationEnvironmentPreparedEvent
:Environment
加载完成,但是上下文未创建发送该事件ApplicationPreparedEvent
:上下文加载完成,但是spring bean未加载完成发送该事件ApplicationReadyEvent
:启动完成发送该事件,整个应用程序处于就绪状态ApplicationFailedEvent
:启动时异常发送该事件Spring Appication
默认是开启Web Environment
,你可以通过setWebEnvironment(false)
来关闭它,比如正在使用SpringApplication
做JUnit
测试。
如果你使用SpringApplication.run(Application.class,args)
来启动一个Spring Application
,然后你又希望你在自己定义的Bean
里用到这些参数,那么你可以通过注入一个ApplicationArguments
来获取,ApplicationArguments
提供了各种各样的方式来获取参数。
|
|
当然,Spring Boot
会根据Environment
来注册一个CommandLinePropertySource
,你可以通过@Value
来获取
ApplicationRunner
或者CommandLineRunner
如果你想在Spring Application
启动时运行自己的一些代码,你可以自己定义一个Bean
,实现接口ApplicationRunner
或者CommandLineRunner
。Spring Application
会在SpringApplication.run(…)
之前去调用他们。
ApplicationRunner
和CommandLineRunner
的区别在于ApplicationRunner
使用了String... args
作为参数,ApplicationRunner
使用了ApplicationArguments
作为参数。
|
|
如果你定义了多个Runner
,然后又希望他们是有序去执行的,两种方式供你选择
org.springframework.core.Ordered
org.springframework.core.annotation.Order
任何一个Spring Application
都会向JVM
注册一个Shutdown Hook
以便ApplicationContext
在Spring Application
退出的时候能够优雅的关闭。
DisposableBean
@PreDestroy
org.springframework.boot.ExitCodeGenerator
,在Spring Application
退出的时候能返回自己定义的exit code