基础知识复习手册
本文设计的知识包括:配置常用注解、Spring Boot 自动配置、配置原理、如何修改默认配置、静态资源处理、Rest 映射、Spring Boot 常用注解、文件上传、拦截器、错误处理、数据层整合:MyBatis、JDBC、Druid、Redis 等等。
配置注解
@Configuration & @Bean
Spring Boot 不同于传统的 Spring,它不提倡使用配置文件,而是使用配置类来代替配置文件,所以该注解就是用于将一个类指定为配置类:@Configuration
public class MyConfig { @Bean("user") public User getUser(){ return new User("张三",20); } }
在配置类中使用方法对组件进行注册,它的效果等价于:
<bean id="user" class="com.wwj.springboot.bean.User"> <property name="name" value="张三"/> <property name="age" value="20"/> </bean>
需要注意的是 Spring Boot 默认会以方法名作为组件的 id,也可以在 @Bean() 中指定 value 值作为组件的 id。
@Import
在 Spring 中,我们可以使用@Component、@Controller、@Service、@Repository 注解进行组件的注册,而对于一些第三方的类,我们无法在类上添加这些注解,为此,我们可以使用@Import 注解将其注册到容器中。@Configuration(proxyBeanMethods = true) @Import(User.class) public class MyConfig { }
通过@Import 注解注册的组件,其 id 为全类名。
@Conditional
该注解为条件装配注解,大量运用于 SpringBoot 底层,由该注解衍生出来的注解非常多:这里以@ConditionalOnBean 和@ConditionalOnMissingBean 举例。其中@ConditionalOnBean 注解的作用是判断当前容器中是否拥有指定的 Bean,若有才生效,比如:
@Configuration public class MyConfig { @Bean("dog") public Dog getDog(){ return new Dog(); } @Bean("user") @ConditionalOnBean(name = "dog") public User getUser(){ return new User("张三",20); } }
若如此,则 SpringBoot 在注册 User 对象之前,会先判断容器中是否已经有 id 为 dog 的对象,若有才创建,否则不创建。@ConditionalOnBean 注解共有三种方式判断容器中是否已经存在指定的对象,除了可以判断组件的 id 外,也能够通过判断组件的全类名:
@Bean("user") @ConditionalOnBean(type = "com.wwj.springboot.bean.Dog") public User getUser(){ return new User("张三",20); }
还可以通过判断组件的类型:
@Bean("user") @ConditionalOnBean(value = Dog.class) public User getUser(){ return new User("张三",20); }
尤其需要注意的是,因为代码是从上至下依次执行的,所以在注册组件时的顺序要特别注意,比如:
@Configuration public class MyConfig { @Bean("user") @ConditionalOnBean(value = Dog.class) public User getUser(){ return new User("张三",20); } @Bean("dog") public Dog getDog(){ return new Dog(); } }
在这段程序中,SpringBoot 会先注册 User 对象,而此时 Dog 对象还没有被注册,所以会导致 User 对象无法注册。
而@ConditionalOnMissingBean 注解的作用与@ConditionalOnBean 注解正好相反,它会判断当前容器中是否不存在指定的 Bean,若不存在则生效,否则不生效。
这些注解除了能够标注在方法上,还能作用于类上,当被标注在类上时,若条件成立,则配置类的所有注册方法生效;若条件不成立,则配置类的所有注册方法均不成立。
@Configuration @ConditionalOnBean(value = Dog.class) public class MyConfig { @Bean("user") public User getUser(){ return new User("张三",20); } @Bean("dog") public Dog getDog(){ return new Dog(); } }
@ImportResource
该注解用于导入资源,比如现在有一个 Spring 的配置文件:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="ls" class="com.wwj.springboot.bean.User"> <property name="name" value="李四"/> <property name="age" value="25"/> </bean> <bean id="tom" class="com.wwj.springboot.bean.Dog"> <property name="name" value="tom"/> <property name="age" value="3"/> </bean> </beans>
若是想将其转化为配置类,代码少一点倒还好说,当配置文件中注册的 Bean 非常多时,采用人工的方式显然不是一个好的办法,为此,SpringBoot 提供了@ImportResource 注解,该注解可以将 Spring 的配置文件直接导入到容器中,自动完成组件注册。
@Configuration @ImportResource("classpath:bean.xml") public class MyConfig { @Bean("user") public User getUser(){ return new User("张三",20); } @Bean("dog") public Dog getDog(){ return new Dog(); } }
@ConfigurationProperties
该注解用于配置绑定,也大量运用于 SpringBoot 底层。首先在配置文件中编写两个键值:user.name=zhangsan user.age=30
然后使用该注解将其绑定到 User 类上:
@Component @ConfigurationProperties(prefix = "user") public class User { private String name; private int age; public User() { } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
但结果却有些出乎意料:
User{name='Administrator', age=30}
这是因为我们将前缀 prefix 指定为了 user,而 user 可能和我们的系统配置产生了重复,所以导致了这个问题,此时我们只需将前缀修改一下即可:
@Component @ConfigurationProperties(prefix = "users") public class User { private String name; private int age; public User() { } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
前缀修改了,配置文件的内容也需要做相应的修改:
users.name=zhangsan users.age=30
需要注意的是,若是想实现配置绑定,就必须要将这个待绑定的类注册到容器中,比如使用@Component 注解,当然,SpringBoot 也提供了一个注解与其配套使用,它就是:@EnableConfigurationProperties 。
该注解必须标注在配置类上:
@Configuration @EnableConfigurationProperties(User.class) public class MyConfig { }
作用是开启指定类的配置绑定功能,它的底层其实也是通过@Import 注解实现的,此时 User 类就无需将其注册到容器中:
@ConfigurationProperties(prefix = "users") public class User { private String name; private int age; public User() { } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
Spring Boot 会自动将属性值绑定到 User 类,并将其注册到容器中。
自动配置原理
有了前面的注解基础之后,我们就能够更深入地了解 Spring Boot 的自动配置原理,自动配置正是建立在这些强大的注解之上的。
我们首先观察一下主启动类上的注解:
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
翻阅源码可以得知,@SpringBootApplication 注解其实是由三个注解组成的:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
}
其中@SpringBootConfiguration 底层是@Configuration 注解,它表示主启动类是一个配置类;而@ComponentScan 是扫描注解,它默认扫描的是主启动类当前包及其子包下的组件;最关键的就是@EnableAutoConfiguration 注解了,该注解便实现了自动配置。
查看@EnableAutoConfiguration 注解的源码,又会发现它是由两个注解组合而成的:
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
}
我们继续查看@AutoConfigurationPackage 注解的源码:
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
@Import 注解我们非常熟悉,它是用来导入一个组件的,然而它比较特殊:
static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {
Registrar() {
}
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
AutoConfigurationPackages.register(registry, (String[])(new AutoConfigurationPackages.PackageImports(metadata)).getPackageNames().toArray(new String[0]));
}
public Set<Object> determineImports(AnnotationMetadata metadata) {
return Collections.singleton(new AutoConfigurationPackages.PackageImports(metadata));
}
}
这里的 Registrar 组件中有两个方法,它是用来导入一系列组件的,而该注解又被间接标注在了启动类上,所以它会将主启动类所在包及其子包下的所有组件均注册到容器中。
接下来我们继续看@EnableAutoConfiguration 的第二个合成注解:@Import({AutoConfigurationImportSelector.class}) 该注解也向容器中注册了一个组件,翻阅该组件的源码:
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered {
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return NO_IMPORTS;
} else {
AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
}
}
这个方法是用来选择导入哪些组件的,该方法又调用了 getAutoConfigurationEntry()方法得到需要导入的组件,所以我们查看该方法:
protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
} else {
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
configurations = this.removeDuplicates(configurations);
Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.getConfigurationClassFilter().filter(configurations);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
}
}
在 getCandidateConfigurations()方法处打一个断点,通过 debug 运行后我们可以发现,configurations 集合中就已经得到了 127 个自动配置类:
那么这些类究竟从何而来呢?我们需要探究一下 getCandidateConfigurations()方法做了什么操作,它其实是调用了 loadFactoryNames()方法:
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
最终调用的是 loadSpringFactories()方法来得到一个 Map 集合:
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
if (result != null) {
return result;
} else {
try {
Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
LinkedMultiValueMap result = new LinkedMultiValueMap();
}
}
}
可以看到,它其实是从 META-INF/spring.factories 文件中获取的组件,我们可以看看导入的依赖中:
在 spring-boot-autoconfigure-2.3.7.RELEASE.jar 的 META-INF 目录下就有一个 spring.factories 文件,打开看看文件内容:
# Initializers
org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer
# Auto Configuration Import Listeners
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\
org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener
# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
......
文件里的内容其实就是在最开始需要注册的组件,这些组件都是一些配置类,只要项目一启动,Spring Boot 就会将这些配置类全部注册到容器中。 按需开启自动配置
虽然配置类会被 Spring Boot 自动注册到容器中,但并不是每个配置类都会默认生效,SpringBoot 会根据当前的场景按需开启自动配置。比如 Thymeleaf 模板引擎的自动配置类:
@ConditionalOnClass 注解的作用是检查当前项目是否有指定的.class 文件,若有则生效;否则不生效。因为我们并未引入 Thymeleaf 的依赖,导致 TemplateMode.class 和 SpringTemplatengine.class 都是不存在的,所以 ThymeleafAutoCinfiguration 并不会生效。
修改默认配置 既然 SpringBoot 帮助我们进行了大量的自动配置,那么对于特殊的一些应用场景,我们该如何修改它的默认配置呢?如果你不了解 SpringBoot 的配置原理,那么当你需要修改默认配置时,你肯定是束手无策的。我们可以找到 SpringMVC 的默认配置,看看 SpringBoot 是如何帮我们进行配置的:
@EnableConfigurationPropertie(WebMvcProperties.class)注解在之前也有介绍,它是用来开启指定类的配置绑定的,所以我们来看看 WebMvcProperties 类:
@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {
}
配置绑定的前缀时 spring.mvc,所以我们若是想修改 SpringBoot 的默认配置,则必须要将前缀写为 spring.mvc,至于我们可以修改哪些配置,只需要查看该类中有哪些成员变量即可,比如:
public static class View {
private String prefix;
private String suffix;
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return this.suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
在 WebMvcProperties 类中有这样一个内部类,内部类中有 prefix 和 suffix 两个成员变量,它们是分别用来设置视图的前缀和后缀的,所以我们若想进行配置,则需要在配置文件中这样编写:
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.html
Web 开发
静态资源处理
Spring Boot默认设置了几个静态资源目录: /static /public /resources /META-INF/resources 这几个目录需要建立在类路径下,若如此做,则放置在这些目录下的静态资源可以被直接访问到。
也可以通过配置来设置资源的访问前缀:
spring.mvc.static-path-pattern=/res
此时若想访问静态资源,就必须添加 res 前缀才行。
我们还可以修改 Spring Boot 的默认资源路径,只需添加配置:
spring.web.resources.static-locations=classpath:/myImg
若如此做,则我们只能将静态资源放在 myImg 目录下,之前的所有静态资源目录都将失效。
欢迎页
Spring Boot 提供了两种方式来实现欢迎页,第一种便是在资源目录放置欢迎页:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <h1>SpringBoot Index!</h1> </body> </html>
第二种方式是通过 Controller 处理/index 请求:
@Controller public class HelloController {
@RequestMapping("/") public String toIndex(){ return "hello"; }
}
Favicon
Spring Boot 也提供了自动设置网站图标的方式,只需要将名为 favicon.ico 的图片放在静态资源目录下即可:
Rest 映射
在 Spring Boot 中,默认已经注册了 HiddenHttpMethodFilter,所以可以直接编写 Rest 风格的 url,只需在表单中添加一个_method 属性的请求域即可:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <form action="/user" method="get"> <input value="Get提交" type="submit" /> </form> <form action="/user" method="post"> <input value="Post提交" type="submit" /> </form> <form action="/user" method="post"> <input type="hidden" name="_method" value="DELETE" /> <input value="Delete提交" type="submit" /> </form> <form action="/user" method="post"> <input type="hidden" name="_method" value="PUT" /> <input value="Put提交" type="submit" /> </form> </body> </html>
编写 Controller 处理请求:
@RestController public class HelloController { @GetMapping("/user") public String getUser(){ return "Get"; } @PostMapping("/user") public String postUser(){ return "Post"; } @DeleteMapping("/user") public String deleteUser(){ return "Delete"; } @PutMapping("/user") public String putUser(){ return "Put"; } }
最后需要在配置文件中开启对 Rest 的支持:
spring.mvc.hiddenmethod.filter.enabled=true
常用参数及注解
下面介绍 Web 开发中的一些常用参数和注解。
@PathVariable
该注解用于获取路径变量,比如:
@GetMapping("/user/{id}") public String getUser(@PathVariable("id") Integer id){ return id + ""; }
此时若请求 url 为 http://localhost:8080/user/2,则获取到 id 值为 2。
@RequestHeader
该注解用于获取请求头,比如:
@GetMapping("/header") public String getHeader(@RequestHeader("User-Agent") String userAgent){ return userAgent; }
它还能够通过一个 Map 集合获取所有的请求头信息:
@GetMapping("/header") public Map<String, String> getHeader(@RequestHeader Map<String,String> headers){ return headers; }
@RequestParam
该注解用于获取请求参数,比如:
@GetMapping("/param") public String getParam(@RequestParam("name") String name, @RequestParam("age") Integer age){ return name + ":" + age; }
此时若请求 url 为 http://localhost:8080/param?name=zhangsan&age=20,则得到值 zhangsan:20 。
@CookieValue
该注解用于获取 Cookie 值,比如:
@GetMapping("/cookie") public String getCookie(@CookieValue("Idea-8296e76f") String cookie) { return cookie; }
它还可以通过 Cookie 键名获取一个 Cookie 对象:
@GetMapping("/cookie") public String getCookie(@CookieValue("Idea-8296e76f") Cookie cookie) { return cookie.getName(); }
@RequestBody
该注解用于获取获取请求体的值,比如:
@PostMapping("/body") public String getBody(@RequestBody String content) { return content; }
既然是获取请求体的值,那么只有 Post 请求才有请求体,所以编写一个表单:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <form action="/body" method="post"> 账号:<input type="text" name="username" /> <br /> 密码:<input type="text" name="password" /> <br /> <input type="submit" value="提交" /> </form> </body> </html>
通过该表单提交数据后,得到 username=admin&password=123 。
@RequestAttribute
该注解用于获取 request 域的数据,比如:
@GetMapping("/success") public String success(@RequestAttribute("msg") String msg){ return msg; }
通过键名即可获取 request 域中的数据。
@MatrixVariable
该注解用于获取矩阵变量,比如:
@GetMapping("/matrix/{path}") public String getMatrix(@MatrixVariable("name") String name, @MatrixVariable("age") Integer age, @PathVariable("path") String path) { return path + "---" + name + ":" + age; }
对于该注解的使用,需要注意几点,首先矩阵变量是绑定在路径中的,所以请求映射中一定要携带一个${path};其次在 SpringBoot 中默认禁用掉了矩阵变量的功能,所以我们还需要手动去开启该功能:
@Configuration public class MyConfig { @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }; } }
此时访问请求 url:http://localhost:8080/matrix/test;name=zhangsan;age=20, 得到结果:test---zhangsan:20 。
拦截器
一个完善的 Web 应用一定要考虑安全问题,比如,只有登录上系统的用户才能查看系统内的资源,或者只有具备相关权限,才能访问对应的资源,为此,我们需要学习一下拦截器,通过拦截器我们就能够实现这些安全认证。
这里以登录检查为例:
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
Object user = session.getAttribute("user");
if(user != null){
return true;
}
response.sendRedirect("/toLogin");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
编写好拦截器后需要将其配置到容器中:
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("toLogin", "/css/**", "/js/**", "/fonts/**", "/images/**");
}
}
需要指定该拦截器需要拦截哪些资源,需要放行哪些资源,这样一个简单的登录校验就完成了。
文件上传
Spring Boot 中该如何实现文件上传呢?现有如下的一个表单:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="f" />
<input type="submit" value="上传" />
</form>
</body>
</html>
编写控制方法:
@RestController
public class FileController {
@PostMapping("/upload")
public String upload(@RequestPart("f") MultipartFile file){
String name = file.getOriginalFilename();
long size = file.getSize();
return name + ":" + size;
}
}
通过@RequestPart 注解即可将上传的文件封装到 MultipartFile 中,通过该对象便可以获取到文件的所有信息。
若是上传多个文件,则先修改表单信息:
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="f" multiple />
<input type="submit" value="上传" />
</form>
在文件框位置添加 multiple 属性即可支持多文件上传,然后修改控制器代码:
@PostMapping("/upload")
public String upload(@RequestPart("f") MultipartFile[] file){
return file.length + "";
}
若是需要将上传的文件保存到服务器,则可以如此做:
@PostMapping("/upload")
public String upload(@RequestPart("f") MultipartFile[] file) throws IOException {
for (MultipartFile multipartFile : file) {
if(!multipartFile.isEmpty()){
String filename = multipartFile.getOriginalFilename();
multipartFile.transferTo(new File("E:\\" + filename));
}
}
return "success";
}
因为 Spring Boot 默认的文件上传大小限制为 1MB,所以只要文件稍微大了一点就会上传失败,为此,可以修改 SpringBoot 的默认配置:
spring.servlet.multipart.max-file-size=30MB # 配置单个文件上传大小限制
spring.servlet.multipart.max-request-size=100MB # 配置总文件上传大小限制
错误处理
默认情况下,SpringBoot 应用出现了异常或错误会自动跳转至/error 页面。
然而一般情况下,我们都不会选择出异常时显示这个页面,而是想要显示我们自己定制的页面,为此,我们可以在/static 或/templates 目录下新建一个 error 目录,并在/error 目录下放置命名为 4xx、5xx 的页面,SpringBoot 会自动帮助我们解析。
此时当出现 5xx 的异常时,SpringBoot 会自动跳转至 5xx.html 页面,当然你也可以对每个状态码都做一个页面进行对应,比如放置 500.html、501.html、502.html 文件,当服务器出现对应的异常时,就会跳转至对应的页面。
数据层
JDBC
若想使用原生的 JDBC 进行开发,SpringBoot 已经为我们配置好了 JDBC 的相关信息,只需要引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency>
Spring Boot 底层自动配置了 HikariDataSource 数据源,所以我们只需指定数据源的地址、用户名和密码即可:
spring.datasource.url=jdbc:mysql: spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
因为 SpringBoot 已经自动配置好了 JdbcTemplate,所以我们直接使用就可以了:
@SpringBootTest class SpringbootApplicationTests { @Autowired private JdbcTemplate jdbcTemplate; @Test void contextLoads() { List<String> names = jdbcTemplate.queryForList("select name from student",String.class); for (String name : names) { System.out.println(name); } } }
Druid
若是不想使用 Spring Boot 底层的数据源,我们也可以修改默认配置,以 Druid 数据源为例,首先引入依赖:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency>
并对 Druid 进行配置:
# 开启Druid的监控页功能 spring.datasource.druid.stat-view-servlet.enabled=true # 开启防火墙功能 spring.datasource.druid.filter-class-names=stat,wall # 配置监控页的用户名和密码 spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=123 # 开启Druid的Web监控功能 spring.datasource.druid.web-stat-filter.enabled=true # 配置监控哪些请求 spring.datasource.druid.web-stat-filter.url-pattern=...
此时访问 http://localhost:8080/druid,将会来到 Druid 的监控页:
MyBatis
接下来我们将整合 MyBatis 框架,并介绍它的简单使用。首先引入依赖:
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
然后编写 Mapper 接口:
@Mapper public interface StudentMapper { Student getStu(Integer id); }
编写 Mappe 配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wwj.springboot.dao.StudentMapper"> <select id="getStu" resultType="com.wwj.springboot.bean.Student"> select * from student where id = #{id} </select> </mapper>
最后配置一下 MyBatis:
# 配置Mapper配置文件的位置 mybatis.mapper-locations=classpath:mappers/*.xml ## 以下是补充的,选择性添加 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/nba?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root # 使用阿里巴巴druid数据源,默认使用自带 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #开启控制台打印sql mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl # mybatis 下划线转驼峰配置,两者都可以 # mybatis.configuration.mapUnderscoreToCamelCase=true mybatis.configuration.map-underscore-to-camel-case=true # 配置扫描 mybatis.mapper-locations=classpath:mappers/*.xml # 实体类所在的包别名 mybatis.type-aliases-package=com.blog.mybatis.domain
这样就可以使用 MyBatis 了:
@SpringBootTest class SpringbootApplicationTests { @Autowired private StudentMapper studentMapper; @Test void contextLoads() { Student stu = studentMapper.getStu(1); System.out.println(stu); } }
Redis
若是想要整合 Redis,也非常地简单,首先引入依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
然后进行配置:
# 连接的那个数据库 spring.redis.database=0 # redis服务的ip地址 spring.redis.host=192.16.18.100 # redis端口号 spring.redis.port=6379 # redis的密码,没设置过密码,可为空 spring.redis.password=
只需要配置 Redis 的主机地址就可以操作 Redis 了,操作步骤如下:
@SpringBootTest class SpringbootApplicationTests { @Autowired private StringRedisTemplate redisTemplate; @Test void contextLoads() { ValueOperations<String, String> operations = redisTemplate.opsForValue(); operations.set("name","zhangsan"); String name = operations.get("name"); System.out.println(name); } }
若是想使用 Jedis 操作 Redis,则需要导入 Jedis 的依赖:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
并配置:
spring.redis.client-type=jedis
以下是 redis 的补充
RedisConfig
@Configuration public class RedisConfig { @Bean public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory){ RedisTemplate<String,String> redisTemplate=new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); redisTemplate.setConnectionFactory(factory); //key序列化 redisTemplate.setKeySerializer(redisSerializer); //value序列化 redisTemplate.setValueSerializer(redisSerializer); //value hashmap序列化 redisTemplate.setHashKeySerializer(redisSerializer); //key hashmap序列化 redisTemplate.setHashValueSerializer(redisSerializer); return redisTemplate; } }
RedisUtils
@Service public class RedisUtils { @Autowired private RedisTemplate redisTemplate; private static double size = Math.pow(2, 32); /** * 写入缓存 * @param key * @param offset 位 8Bit=1Byte * @return */ public boolean setBit(String key, long offset, boolean isShow) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.setBit(key, offset, isShow); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param offset * @return */ public boolean getBit(String key, long offset) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.getBit(key, offset); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存设置时效时间 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 哈希 添加 * * @param key * @param hashKey * @param value */ public void hmSet(String key, Object hashKey, Object value) { HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); hash.put(key, hashKey, value); } /** * 哈希获取数据 * * @param key * @param hashKey * @return */ public Object hmGet(String key, Object hashKey) { HashOperations<String, Object, Object> hash = redisTemplate.opsForHash(); return hash.get(key, hashKey); } /** * 列表添加 * * @param k * @param v */ public void lPush(String k, Object v) { ListOperations<String, Object> list = redisTemplate.opsForList(); list.rightPush(k, v); } /** * 列表获取 * * @param k * @param l * @param l1 * @return */ public List<Object> lRange(String k, long l, long l1) { ListOperations<String, Object> list = redisTemplate.opsForList(); return list.range(k, l, l1); } /** * 集合添加 * * @param key * @param value */ public void add(String key, Object value) { SetOperations<String, Object> set = redisTemplate.opsForSet(); set.add(key, value); } /** * 集合获取 * * @param key * @return */ public Set<Object> setMembers(String key) { SetOperations<String, Object> set = redisTemplate.opsForSet(); return set.members(key); } /** * 有序集合添加 * * @param key * @param value * @param scoure */ public void zAdd(String key, Object value, double scoure) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); zset.add(key, value, scoure); } /** * 有序集合获取 * * @param key * @param scoure * @param scoure1 * @return */ public Set<Object> rangeByScore(String key, double scoure, double scoure1) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); redisTemplate.opsForValue(); return zset.rangeByScore(key, scoure, scoure1); } //第一次加载的时候将数据加载到redis中 public void saveDataToRedis(String name) { double index = Math.abs(name.hashCode() % size); long indexLong = new Double(index).longValue(); boolean availableUsers = setBit("availableUsers", indexLong, true); } //第一次加载的时候将数据加载到redis中 public boolean getDataToRedis(String name) { double index = Math.abs(name.hashCode() % size); long indexLong = new Double(index).longValue(); return getBit("availableUsers", indexLong); } /** * 有序集合获取排名 * * @param key 集合名称 * @param value 值 */ public Long zRank(String key, Object value) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); return zset.rank(key,value); } /** * 有序集合获取排名 * * @param key */ public Set<ZSetOperations.TypedTuple<Object>> zRankWithScore(String key, long start,long end) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); Set<ZSetOperations.TypedTuple<Object>> ret = zset.rangeWithScores(key,start,end); return ret; } /** * 有序集合添加 * * @param key * @param value */ public Double zSetScore(String key, Object value) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); return zset.score(key,value); } /** * 有序集合添加分数 * * @param key * @param value * @param scoure */ public void incrementScore(String key, Object value, double scoure) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); zset.incrementScore(key, value, scoure); } /** * 有序集合获取排名 * * @param key */ public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithScore(String key, long start,long end) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); Set<ZSetOperations.TypedTuple<Object>> ret = zset.reverseRangeByScoreWithScores(key,start,end); return ret; } /** * 有序集合获取排名 * * @param key */ public Set<ZSetOperations.TypedTuple<Object>> reverseZRankWithRank(String key, long start, long end) { ZSetOperations<String, Object> zset = redisTemplate.opsForZSet(); Set<ZSetOperations.TypedTuple<Object>> ret = zset.reverseRangeWithScores(key, start, end); return ret; } }
springboot 启动为所欲为
@PostConstruct
@Component public class CustomBean { @Autowired private Environment env; @PostConstruce // Bean 初始化之后实现相应的初始化逻辑 public void init() { env.getActiveProfiles(); } @PreDestroy // 想在 Bean 注销时完成一些清扫工作,如关闭线程池等,可以使用@PreDestroy注解: public void destroy() { env.getActiveProfiles(); } }
InitializingBean
@Component public class CustomBean implements InitializingBean { private static final Logger LOG = Logger.getLogger(InitializingBeanExampleBean.class); @Autowired private Environment environment; @Override // Bean 初始化完成之后执行 public void afterPropertiesSet() throws Exception { LOG.info(environment.getDefaultProfiles()); } }
ApplicationListener
@Component @Slf4j public class StartupApplicationListenerExample implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { log.info("Subject ContextRefreshedEvent"); } } // 或者通过@EventListener注解来监听相对应事件: @Component @Slf4j public class StartupApplicationListenerExample { @EventListener public void onApplicationEvent(ContextRefreshedEvent event) { log.info("Subject ContextRefreshedEvent"); } }
Constructor
@Component @Slf4j public class ConstructorBean { private final Environment environment; @Autowired public LogicInConstructorExampleBean(Environment environment) { this.environment = environment; log.info(Arrays.asList(environment.getDefaultProfiles())); } }
CommandLineRunner
// 多个CommandLineRunner实现,可以通过@Order来控制它们的执行顺序。 @Component @Slf4j public class CommandLineAppStartupRunner implements CommandLineRunner { @Override public void run(String...args) throws Exception { log.info("Increment counter"); } }
SmartLifecycle
还有一种更高级的方法来实现我们的逻辑。这可以 Spring 高级开发必备技能哦。
@Component public class SmartLifecycleExample implements SmartLifecycle { private boolean isRunning = false; //bean 初始化完毕后,该方法会被执行。 @Override public void start() { System.out.println("start"); isRunning = true; } // 控制多个 SmartLifecycle 的回调顺序的,返回值越小越靠前执行 start() 方法,越靠后执行 stop() 方法。 @Override public int getPhase() { // 默认为 0 return 0; } // start 方法被执行前先看此方法返回值,返回 false 就不执行 start 方法了。 @Override public boolean isAutoStartup() { // 默认为 false return true; } // 当前状态,用来判你的断组件是否在运行。 @Override public boolean isRunning() { // 默认返回 false return isRunning; } // 容器关闭后,spring 容器发现当前对象实现了 SmartLifecycle,就调用 stop(Runnable), 如果只是实现了 Lifecycle,就调用 stop()。 @Override public void stop(Runnable callback) { System.out.println("stop(Runnable)"); callback.run(); isRunning = false; } @Override public void stop() { System.out.println("stop"); isRunning = false; } }