Spring项目转SpringBoot
Spring致力于简化Java开发,而Spring Boot致力于让Spring本身更加简单。
前一篇博客:整合SpringMVC框架中的简易论坛登录项目使用的是Spring+Mybatis+SpringMVC框架进行搭建的,本篇博客旨在使用Spring Boot进行优化,简化Spring的相关配置。
Spring Boot用了两个技巧来消除Spring项目中的样板式配置:Spring Boot Starter和自动配置。
- Spring Boot Starter依赖的内部原理使用了Maven和Gradle的依赖传递方案。当我们将某一个starter依赖添加到Maven或Gradle构建中的时候,Starter的依赖将会自动地传递性解析。这些依赖本身可能也会有其他的依赖。
- 自动配置充分利用了Spring4.0的条件化配置特性,能够自动配置特定的Spring bean,用来启用某项特性。例如,Spring Boot能够在应用的类路径中探测到Thymeleaf,然后自动将Thymeleaf模板配置为Spring MVC视图的bean。
1、pom.xml中配置相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.smarter</groupId>
<artifactId>simple</artifactId>
<version>1.0-SNAPSHOT</version>
<!--继承Spring Boot默认配置-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--添加一个Boot Web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<!--配置Spring Boot运行插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 继承Spring Boot提供的根默认配置依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
并且指定当前版本号为2.2.2.RELEASE,这里引入spring-boot-starter-parent的好处是在添加启动器时不必再声明版本号。
- 添加spring-boot-starter-web启动器依赖,不再像xxxxSpring版本一样,需要引入很多Spring子模块依赖。因为spring-boot-starter-web内部已经封装了spring-web、spring-webmvc、jackson-databind等模块依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 配置Spring Boot运行插件。刷新IDEA开发工具右边的Maven Projects选项卡,就可以看到Spring Boot运行命令:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 要使用Spring Boot提供的JDBC启动器,要引入spring-boot-starter-jdbc依赖及访问数据库的JDBC驱动器依赖(mysql-connector-java):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
2、配置application.properties
application.properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sampledb?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
#--initialize mysql scripts
#spring.datasource.initialize=true
#spring.datasource.platform=mysql
#spring.datasource.data=data
#spring.datasource.schema=schema
#spring.datasource.max-wait=10000
#spring.datasource.max-active=50
#spring.datasource.max-idle=10
#spring.datasource.min-idle=8
#spring.datasource.test-on-borrow=true
#spring.datasource.validation-query=select 1
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
- 在资源根目录(main/resources/application.properties文件)中配置数据库的连接信息,这样Spring Boot就能够自动装配数据源的连接。
application.properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/sampledb?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
- 配置MVC视图映射:在application.properties中配置视图解析规则(前缀、后缀):
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
3、配置Application类
我们需要有个特殊的类来启动Spring Boot应用,Spring本身并不知道自动配置的任何信息。Application类就是Spring Boot启动类。
Application.java:
package com.smarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.WebApplicationInitializer;
import javax.sql.DataSource;
@SpringBootApplication
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {
// @Bean
// public PlatformTransactionManager txManger(DataSource dataSource){
// return new DataSourceTransactionManager(dataSource);
// }
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
- 在Application类上标注@EnableTransactionManagement注解,Boot为应用自动装配了事务支持。这对用户并不透明,用户如果想自己定义事务管理器,则在Application类中添加一个,在Application中添加自定义事务管理器方法txManager(),并在方法上标注@Bean注解,此时Spring Boot会加载自定义的事务管理器,不会重新实例化其他事务管理器:
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer implements WebApplicationInitializer {
@Bean
public PlatformTransactionManager txManger(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
... ...
}
- Application类中main()方法会告诉Spring Boot(通过SpringApplication类)根据Application中的配置以及命令行中的参数来运行。
- 在Boot环境中配置MVC,只要在Application继承Spring Boot提供的Servlet初始化器SpringBootServletInitializer,重写SpringBootServletInitializer的configure()方法:
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
参考
- 《精通Spring4.x——企业应用开发实战》
本项目github地址:https://github.com/bluJoker/forumSSM.git 分支:springboot
版本:v6.0