自定义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-webspring-social-configspring-social-corespring-social-security目前已正式发布支持的有Facebook,Twitter,LinkedIn
spring-social-linkedinspring-social-facebookspring-social-twitter正在孵化的有Github和Tripit
spring-social-githubspring-social-tripitspring-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下的一款开源的JTAbitronix: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的Tomcat8080INFOPID)为46716Spring Boot从1.4版本之后新增了FailureAnalyzer接口,它可以拦截异常,并将其转换为更易读的诊断信息。比如当Spring Application试图使用已被占用的端口时,将显示如下信息
|
|
Spring Boot-1.4.2-RELEASE版本内置了以下几个FailureAnalyzer
BeanCurrentlyInCreationFailureAnalyzerBeanNotOfRequiredTypeFailureAnalyzerBindFailureAnalyzerConnectorStartFailureAnalyzerNoUniqueBeanDefinitionFailureAnalyzerPortInUseFailureAnalyzerValidationExceptionFailureAnalyzer你可以从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.Orderedorg.springframework.core.annotation.Order任何一个Spring Application都会向JVM注册一个Shutdown Hook以便ApplicationContext在Spring Application退出的时候能够优雅的关闭。
DisposableBean@PreDestroyorg.springframework.boot.ExitCodeGenerator,在Spring Application退出的时候能返回自己定义的exit code