Spring-boot 的 2 大优点:
1. 基于 Spring 框架的 "约定优先于配置(COC)" 理念以及最佳实践之路.
2. 针对日常企业应用研发各种场景的 Spring-boot-starter 自动配置依赖模块,且 "开箱即用"(约定 spring-boot-starter- 作为命名前缀,都位于 org.springframenwork.boot 包或者命名空间下).
应用日志和 spring-boot-starter-logging
常见的日志系统大致有:java.util 默认提供的日志支持,log4j,log4j2,commons logging, 下面的 spring-boot-starter-logging 也是其中的一种.
maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
springBoot 将使用 logback 作为应用日志的框架,程序启动时,由 org.springframework.boot.logging-Logging-Application-Lisetener 根据情况初始化并使用.
如果要想改变 springBoot 提供的应用日志设定,可以通过一下原则:
遵循 logback 的约定,在 classpath 中使用自己定制的 logback.xml 配置文件.
在文件系统的任意一个位置提供自己的 logback.xml 配置文件,然后通过 logging.config 配置项指向这个配置文件然后引用它,例如在 application.properties 中指定如下的配置:
logging.config=/{some.path.you.defined}/any-logfile-name-I-like.log}
快速 web 应用开发与 spring-boot-starter-web
maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在当下项目运行 mvn spring-boot:run 就可以直接启用一个嵌套了 tomcat 的 web 应用.
如果没有提供任何服务的 Cotroller, 访问任何路径都会返回一个 springBoot 默认的错误页面(Whitelabel error page).
嵌入式 Web 容器层面的约定和定制
spring-boot-starter-web 默认使用嵌套式的 Tomcat 作为 Web 容器对外提供 HTTP 服务,默认端口 8080 对外监听和提供服务.
我们同样可以使用 spring-boot-starter-jetty 或者 spring-boot-starter-undertow 作为 Web 容器.
想改变默认的配置端口,可以在 application.properties 中指定:
server.port = 9000(the port number you want)
类似的配置还有:
server.address
server.ssl.*
server.tomcat.*
如果上诉仍然没有办法满足要求,springBoot 支持对嵌入式的 Web 容器实例进行定制,可以通过向 IoC 容器中注册一个 EmbeddedServletContainerCustomizer 类型的组件来对嵌入式的 Web 容器进行定制
public class UnveilSpringEmbeddedTomcatCustomizer implements EmbeddedServletContainer{
public void customize(ConfigurableEmbeddedServletContainer container){
container.setPort(9999);
container.setContextPath("C\\hello");
...
}
}
数据访问与 spring-boot-starter-jdbc
maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
默认情况下,当我们没有配置任何 DataSource,SpringBoot 会为我们自动配置一个 DataSource,这种自动配置的方式一般适用于测试,开发还是自己配置一个 DataSource 的实例比较好.
如果我们的工程只依赖一个数据库,那么,使用 DataSource 自动配置模块提供的参数是最方便的:
spring.datasource.url=jdbc:mysql://{datasource host}:3306/{databaseName}
spring.datasource.username={database username}
spring.datasource.passwd={database passwd}
还会自动配置的有:JdbcTemplate DateSourceTransactionManager 等,我们只要在使用的时候注入(@Autowired)就好了
此外,SpringBoot 还支持的数据库有 spring-boot-data-jpa spring-boot-data-mongodb
spring-boot-starter-aop 应用及其使用场景
AOP:Aspect Oriented Programming, 面向切面编程
maven 依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
spring-boot-starter-aop 主要由 2 部分组成:
1. 位于 spring-boot-autoconfigure 的 org.sringframework.boot.autoconfigure.aop.AopAutoConfiguration 提供的 @Configuration 配置类和相应的配置项,即下面的 2 个配置项:
spring.aop.auto=true
spring.aop.proxy-target-class=false
2.spring-boot-starter-aop 模块提供了针对 spring-aop aspectjrt 和 aspectjweaver 的依赖
应用安全与 spring-boot-starter-security //todo
程序媛小白一枚,如有错误,烦请批评指正!(#.#)
来源: http://www.jianshu.com/p/2093dd0168b9