一, 基本配置
1), 入口类和@SpringBootApplicaiton
通常有个*Application的入口类,里面有个main方法,启动Spring Boot应用项目.
@SpringBootApplication 是一组注解(@Configuration, @EnableAutoConfiguration, @ComponentScan)的组合.
2), 关闭特定的自动配置
使用注解的exclude参数.
e.g. @SpringBootApplication(exclude={DataSourceAutoApplication.class})
3), 定制Banner
a, 在Spring Boot 启动的时候有一个默认启动图案.
b, 我们在src/main/resources下新建一个banner.txt
c, 通过http://patorjk.com/software/taag网站生成字符,将生成的字符复制到banner.txt中
d, 启动程序就是修改后的图案
4), 关闭banner
a, main 修改
app = new SpringApplication(xx.class);
app.setShowBanner(false);
app.run(args);
b, 使用fluent API修改
new SpringApplicationBuilder(xx.class)
.showBanner(false)
.run(args);
5), Spring Boot 配置文件
全局配置文件 application.properties 或application.yml
放在src/main/resources目录或类路径的/config下
备注: IDEA中 的@PropertySource注解不支持加载yaml文件
6), starter POMs
名称 描述
spring-boot-starter核心Spring Boot starter,包括自动配置支持,日志和YAML
spring-boot-starter-actuator生产准备的特性,用于帮你监控和管理应用
spring-boot-starter-amqp对”高级消息队列协议”的支持,通过spring-rabbit实现
spring-boot-starter-aop对面向切面编程的支持,包括spring-aop和AspectJ
spring-boot-starter-batch对Spring Batch的支持,包括HSQLDB数据库
spring-boot-starter-cloud-connectors对Spring Cloud Connectors的支持,简化在云平台下(例如,Cloud Foundry 和Heroku)服务的连接
spring-boot-starter-data-elasticsearch对Elasticsearch搜索和分析引擎的支持,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire对GemFire分布式数据存储的支持,包括spring-data-gemfire
spring-boot-starter-data-jpa对”Java持久化API”的支持,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb对MongoDB NOSQL数据库的支持,包括spring-data-mongodb
spring-boot-starter-data-rest对通过REST暴露Spring Data仓库的支持,通过spring-data-rest-webmvc实现
spring-boot-starter-data-solr对Apache Solr搜索平台的支持,包括spring-data-solr
spring-boot-starter-freemarker对FreeMarker模板引擎的支持
spring-boot-starter-groovy-templates对Groovy模板引擎的支持
spring-boot-starter-hateoas对基于HATEOAS的RESTful服务的支持,通过spring-hateoas实现
spring-boot-starter-hornetq对”Java消息服务API”的支持,通过HornetQ实现
spring-boot-starter-integration对普通spring-integration模块的支持
spring-boot-starter-jdbc对JDBC数据库的支持
spring-boot-starter-jersey对Jersey RESTful Web服务框架的支持
spring-boot-starter-jta-atomikos对JTA分布式事务的支持,通过Atomikos实现
spring-boot-starter-jta-bitronix对JTA分布式事务的支持,通过Bitronix实现
spring-boot-starter-mail对javax.mail的支持
spring-boot-starter-mobile对spring-mobile的支持
spring-boot-starter-mustache对Mustache模板引擎的支持
spring-boot-starter-redis对REDIS键值数据存储的支持,包括spring-redis
spring-boot-starter-security对spring-security的支持
spring-boot-starter-social-facebook对spring-social-facebook的支持
spring-boot-starter-social-linkedin对spring-social-linkedin的支持
spring-boot-starter-social-twitter对spring-social-twitter的支持
spring-boot-starter-test对常用测试依赖的支持,包括JUnit, Hamcrest和Mockito,还有spring-test模块
spring-boot-starter-thymeleaf对Thymeleaf模板引擎的支持,包括和Spring的集成
spring-boot-starter-velocity对Velocity模板引擎的支持
spring-boot-starter-web对全栈web开发的支持,包括Tomcat和spring-webmvc
spring-boot-starter-websocket对WebSocket开发的支持
spring-boot-starter-ws对Spring Web服务的支持
7), xml 配置
SB 建议零配置,如果需要可以通过@ImportResource来加载xml配置
二, 外部配置
1), 常规属性配置
@Value: 注入application.properties里面的值
e.g. @Value("${demo.name}")
2), 类型安全的配置
@Component
@ConfigurationProperties(Prefix="demo")
//@ConfigurationProperties(Prefix="demo", locations={"classpath:config/demo.properites"})
Public class demo{
private String name;
get,set......
}
备注: V1.5以上去除了locations这个参数,可通过其他方式实现
三, 日志配置
默认情况下, SB使用了LogBack作为日志框架
配置日志级别: logging.file = d:/log.log
logging.level.org.springframework.web=DEBUG
四, Profile配置
Profile是Spring用来针对不同环境对不同配置的支持
全局Profile使用 applicaiton-{profile}.properties
通过applicaiton.properties中设置spring.profiles.active=prod来指定活动.
五, Spring Boot 运行原理
- @ConditionalOnBean:当前容器有指定Bean的条件下。
- @ConditionalOnClass:当前类路径下有指定的类的条件下。
- @ConditionalOnExpression:基于SpEL表达式作为判断条件。
- @ConditionalOnJava:基于JVM版本作为判断条件。
- @ConditionalOnJndi:在JNDI存在的条件下查找指定的位置。
- @ConditionalOnMissingBean:当容器里没有指定Bean的情况下。
- @ConditionalOnMissingClass:当类路径下没有指定的类的条件下。
- @ConditionalOnNotWebApplication:当前项目不是WEB项目的条件下。
- @ConditionalOnProperty:指定属性是否有指定的值。
- @ConditionalOnResource:类路径是否有指定的值。
- @ConditionalOnSingleCandidate:当指定Bean在容器中只有一个,或者虽然有多个但 是指定首选的Bean。
- @ConditionalOnWebApplication:当前项目是WEB项目的条件下。
六, 示例
附件为hello world demo.
备注:
1),如果没有在application.properties里面配置hello.msg, 那么结果就是缺省 hello world
2), spring.factories文件中, 如果想要有多个自动配置,则用","隔开,里面的"\"是为了换行后仍可读取属性
3), 不要忘了再pom.xml添加依赖
4), 在application.properties中添加debug=true,可查看自动配置报告