1. SpringBoot介绍
Spring Boot是Spring社区较新的一个项目。该项目的目的是帮助开发者更容易的创建基于Spring的应用程序和服务,让更多人的人更快的对Spring进行入门体验,让Java开发也能够实现Ruby on Rails那样的生产效率。为Spring生态系统提供了一种固定的、约定优于配置风格的框架。
Spring Boot具有如下特性:
1.为基于Spring的开发提供更快的入门体验
2.开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
3.提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
4.Spring Boot并不是不对Spring功能上的增强,而是提供了一种快速使用Spring的方式。
Spring 的Java配置方式:
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置。
Spring的Java配置方式是通过 @Configuration 和 @Bean 这两个注解实现的:
-@Configuration 作用于类上,相当于一个xml配置文件;
-@Bean 作用于方法上,相当于xml配置中的<bean>;
最佳实践:
应用的基本配置用xml,比如:数据源、资源文件等;
业务开发用注解,比如:Service中注入bean等;
2. SpringBoot核心
2.1. 入口类
Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法。
@SpringBootApplication注解是Spring Boot的核心注解,它其实是一个组合注解包含SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
- @Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
- @EnableAutoConfiguration:启用自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项
- @ComponentScan:默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。
2.2. 自动配置
Spring Boot会根据项目中的jar包依赖,自动做出配置,Spring Boot支持的自动配置如下(spring-boot-autoconfigure-1.5.7.RELEASE.jar):
2.3. 全局配置文件
Spring Boot项目使用一个全局的配置文件application.properties或者是application.yml,在resources目录下或者类路径下的/config下,一般我们放到resources下。
具体配置参考官网:
https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/#auto-configuration-classes-from-autoconfigure-module
2.4. Starter Pom
3. 快速入门
3.1. 框架目标与环境
以springboot为基础框架搭建目标,功能要求:
a) 处理http/json请求
b) 整合mybatis
c) 事务
d) 日志处理
搭建环境:
a) OS: Mac
b) 编译器:IDEA 2017.2.1
c) JDK 1.8
d) Maven 3.5.0
e) spring-boot 1.5.7.RELEASE
f) mysql 5.7.19
3.2. Hello SpringBoot
3.2.1. 初始化项目
- 访问https://start.spring.io,选择需要的第三方框架,初始化SpringBoot项目:
- 打开下载的文件:
- 项目初始结构如下:
4.删除多余文件,干净目录:
3.2.2. 设置Spring Boot的parent
增加父pom比较简单,而且spring-boot-starter-parent包含了大量配置好的依赖管理,在自己项目添加这些依赖的时候不需要写<version>版本号。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/>
</parent>
使用父pom虽然简单,但是有些情况我们已经有父pom,不能直接增加<parent>时,可以通过如下方式:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3.2.3. 导入Spring Boot web依赖
<!--Spring Boot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.2.4. 导入Spring Boot 插件
<!--Spring Boot Web-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
3.2.5. 编写SpringBoot启动入口
3.2.6. 新增HelloController,进行Hello SpringBoot测试
SpringBoot启动方式
- 方式一:
main方法启动
- 方式二:
maven springloaded插件启动,支持热部署,修改后重新mvn:compile。
- 方式三:
IDEA JRebel 插件,支持热部署,因为篇幅,不深入了,请自行百度。
启动界面:
4. 整合日志
- pom中引入日志依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
- resources下添加logback-spring.xml日志文件(配置文件就不贴了,会放到工程中),全局配置文件application.properties中添加属性:
logging.config=classpath:logback-spring.xml
注:官网建议logback配置文件不要定义为logback.xml,而是推荐logback-spring.xml,如果使用logback,Spring无法完全控制日志初始化。
- 测试
- 彩色日志(需要main方式启动)
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
<!-- 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>
5. 整合 Mybatis
Mybatis和Spring Boot的整合有两种方式:
第一种:使用mybatis官方提供的Spring Boot整合包实现,地址:https://github.com/mybatis/spring-boot-starter
第二种:使用mybatis-spring整合的方式,也就是我们传统的方式
这里我们推荐使用第二种,因为这样我们可以很方便的控制Mybatis的各种配置。
5.1. 准备测试数据
DROP TABLE IF EXISTS `userinfo`;
CREATE TABLE `userinfo` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(50) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
5.2. Pom引入相关jar
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- bonecp 连接池 -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql 驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!--mybatis generator-->
<dependency>
<groupId>mybatis</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.1</version>
</dependency>
5.3. 通过mybatis Generator自动生成代码项目结构如下:
5.4. 新建MyBatisConfig,关联mybatis xml文件
5.5. 修改EngineApplication启动类,配置DateSource,新增Jdbc配置文件
5.6. 测试
6. 增加事务
在Spring Boot中推荐使用@Transactional注解来申明事务
当引入jdbc依赖之后,Spring Boot会自动默认分别注入DataSourceTransactionManager或JpaTransactionManager,所以我们不需要任何额外配置就可以用@Transactional注解进行事务的使用。
在Service中添加@Transactional注解:
- 发布到tomcat运行
- 修改打包方式为war
- 添加tomcat依赖,打包排除
说明:内置tomcat,在引入spring-boot-starter-web依赖时,会自动引入,所以加上scope打包时排除
- 修改启动配置
继承SpringBootServletInitializer,重写configure方法
- 打包部署到tomcat Root目录下
解压jar –vxf engine-0.0.1-SNAPSHOT.war
- 启动tomcat
- 测试:
8. 项目地址
Github: https://github.com/xc398992382/engine
9. 参考
http://blog.csdn.net/u013187139/article/details/68944972
http://blog.csdn.net/isea533/article/details/50359390