SpringBoot遗忘知识点整理
- @PropertySource
- @Value
- OncePerRequestFilter
- Properties
- MIME类型
- Spring MVC好用工具介绍:UrlPathHelper、WebUtils、RequestContextUtils、WebApplicationContextUtils...
- Spring的@Order注解或者Ordered接口,不决定Bean的加载顺序和实例化顺序,只决定Bean的执行顺序
- @RequestHeader
- @JsonFormat与@DateTimeFormat注解的使用
- 坑===> @DateTimeFormat无效原因
- @ConditionalOnProperty
- TIMESTAMP设置为CURRENT_TIMESTAMP 不能自动插入当前时间
- @transient注解
- Spring Boot中注解@ConfigurationProperties的三种使用场景
- @ControllerAdvice处理全局异常,ModelAttribute,InitBinder
- @ResponseStatus设置HTTP状态码
- ResponseBodyAdvice
- RequestBodyAdvice
- Redis-设置Key的过期时间及相关策略
- SpringBoot自定义配置
- Jackson 树模型(tree model)
- SpringMVC之RequestContextHolder分析
- Java用户关闭页面,清除用户Session【服务端】
- SpringBoot使用@ConstructorBinding注解进行配置属性绑定
- ComponentScan加载主配置类上会覆盖默认的包扫描路径
@PropertySource
Spring高级之注解@PropertySource详解(超详细)
@PropertySource配置的用法
@Value
【Spring注解驱动开发】如何使用@Value注解为bean的属性赋值,我们一起吊打面试官!
OncePerRequestFilter
OncePerRequestFilter 过滤器
Spring MVC应用 – 过滤器
SpringBoot基础篇(五)过滤器OncePerRequestFilter
Properties
Properties
MIME类型
MIME类型
Spring MVC好用工具介绍:UrlPathHelper、WebUtils、RequestContextUtils、WebApplicationContextUtils…
Spring MVC好用工具介绍:UrlPathHelper、WebUtils、RequestContextUtils、WebApplicationContextUtils…
Spring的@Order注解或者Ordered接口,不决定Bean的加载顺序和实例化顺序,只决定Bean的执行顺序
深入理解Spring的@Order注解和Ordered接口
@RequestHeader
@RequestHeader
@JsonFormat与@DateTimeFormat注解的使用
https://blog.csdn.net/eeeeasy/article/details/81201819
@DateTimeFormat 和 @JsonFormat 注解
坑===> @DateTimeFormat无效原因
一般都是使用@DateTimeFormat把传给后台的时间字符串转成Date,使用@JsonFormat把后台传出的Date转成时间字符串,
但是@DateTimeFormat只会在类似@RequestParam的请求参数(url拼接的参数才生效,如果是放到RequestBody中的form-data也是无效的)上生效,
如果@DateTimeFormat放到@RequestBody下是无效的。
在@RequestBody中则可以使用@JsonFormat把传给后台的时间字符串转成Date,也就是说@JsonFormat其实既可以把传给后台的时间字符串转成Date也可以把后台传出的Date转成时间字符串。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date time;
@ConditionalOnProperty
在spring boot中有时候需要控制配置类是否生效,可以使用@ConditionalOnProperty注解来控制@Configuration是否生效.
@Configuration
@ConditionalOnProperty(prefix = "filter",name = "loginFilter",havingValue = "true")
public class FilterConfig {
//prefix为配置文件中的前缀,
//name为配置的名字
//havingValue是与配置的值对比值,当两个值相同返回true,配置类生效.
@Bean
public FilterRegistrationBean getFilterRegistration() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean(new LoginFilter());
filterRegistration.addUrlPatterns("/*");
return filterRegistration;
}
配置文件中的代码
filter.loginFilter=true
总结:
通过@ConditionalOnProperty控制配置类是否生效,可以将配置与代码进行分离,实现了更好的控制配置.
@ConditionalOnProperty实现是通过havingValue与配置文件中的值对比,返回为true则配置类生效,反之失效.
TIMESTAMP设置为CURRENT_TIMESTAMP 不能自动插入当前时间
这个问题其实跟数据库是否设置not null没有关系,而是要确保insert语句中没有该字段(mybatis),或者实体bean中不包含该字段(tkmybatis)
如果实体bean中需要该字段怎么办?
字段上用@transient注解
@transient注解
java 的Transient关键字的作用是需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。
用法,@Transient 就是在给某个javabean上需要添加个属性,但是这个属性你又不希望给存到数据库中去,仅仅是做个临时变量,用一下。不修改已经存在数据库的数据的数据结构。
那么这个注解就可以一用。
只要在你准备添加的临时属性上添加这个注解,然后getter和setter自动完成一下,就可以啦。`
Transient使用小结
- 一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。
- transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。
- 被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。
@Transient这个注解一般是用来放在某些数据库中不存在的字段但是你又希望用到的数据上,比如:
这个spu是不存在在我的数据库表中的,但是我又需要使用.就是在这种情况下才会用到@Transient注解
重点来了:
就像是说---->添加了@Transient注解的数据将会加载到使用者的电脑内存中,而不会被加载到磁盘里持久化,不保存数据,但是使用的时候一直存在.(个人理解)
Spring Boot中注解@ConfigurationProperties的三种使用场景
场景一
使用@ConfigurationProperties和@Component注解到bean定义类上,这里@Component代指同一类实例化Bean的注解。
基本使用实例如下:
// 将类定义为一个bean的注解,比如 @Component,@Service,@Controller,@Repository
// 或者 @Configuration
@Component
// 表示使用配置文件中前缀为user1的属性的值初始化该bean定义产生的的bean实例的同名属性
// 在使用时这个定义产生的bean时,其属性name会是Tom
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
// 省略getter/setter方法
}
对应application.properties配置文件内容如下:
user1.name=Tom
在此种场景下,当Bean被实例化时,@ConfigurationProperties会将对应前缀的后面的属性与Bean对象的属性匹配。符合条件则进行赋值。
场景二
使用@ConfigurationProperties和@Bean注解在配置类的Bean定义方法上。以数据源配置为例:
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
这里便是将前缀为“spring.datasource.primary”的属性,赋值给DataSource对应的属性值。
@Configuration注解的配置类中通过@Bean注解在某个方法上将方法返回的对象定义为一个Bean,并使用配置文件中相应的属性初始化该Bean的属性。
场景三
使用@ConfigurationProperties注解到普通类,然后再通过@EnableConfigurationProperties定义为Bean。
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
// 省略getter/setter方法
}
这里User对象并没有使用@Component相关注解。
而该User类对应的使用形式如下:
@SpringBootApplication
@EnableConfigurationProperties({User.class})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
上述代码中,通过@EnableConfigurationProperties对User进行实例化时,便会使用到@ConfigurationProperties的功能,对属性进行匹配赋值
@ControllerAdvice处理全局异常,ModelAttribute,InitBinder
@ControllerAdvice 的介绍及三种用法
@ResponseStatus设置HTTP状态码
@ResponseStatus的作用
SpringMVC入门终结篇
@ResponseStatus注解工作的前提是,上面没有@ExceptionHandler标注的异常处理方法能处理该异常,否则走@ExceptionHandler标注的异常处理方法
ResponseBodyAdvice
ResponseBodyAdvice接口是在Controller执行return之后,在response返回给浏览器或者APP客户端之前,执行的对response的一些处理。可以实现对response数据的一些统一封装或者加密等操作。
spring mvc之@ResponseBodyAdvice使用
@ControllerAdvice
public class GlobalResponseAdvice implements ResponseBodyAdvice {
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
//return returnType.hasMethodAnnotation(ResponseBody.class);
return true;
}
@Override
public Object beforeBodyWrite(Object body,
MethodParameter returnType,
MediaType selectedContentType,
Class selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response) {
//如果响应结果是JSON数据类型
if(selectedContentType.equalsTypeAndSubtype(
MediaType.APPLICATION_JSON)){
//为HTTP响应结果设置状态码,状态码就是AjaxResponse的code,二者达到统一
response.setStatusCode(
HttpStatus.valueOf(((AjaxResponse) body).getCode())
);
return body;
}
return body;
}
}
RequestBodyAdvice
在实际项目中 , 往往需要对请求参数做一些统一的操作 , 例如参数的过滤 , 字符的编码 , 第三方的解密等等 , Spring提供了RequestBodyAdvice一个全局的解决方案 , 免去了我们在Controller处理的繁琐 .
RequestBodyAdvice仅对使用了@RqestBody注解的生效 , 因为它原理上还是AOP , 所以GET方法是不会操作的.
/**
* @title 全局请求参数处理类
* @author Xingbz
* @createDate 2019-8-2
*/
@ControllerAdvice(basePackages = "com.xbz.controller")//此处设置需要当前Advice执行的域 , 省略默认全局生效
public class GlobalRequestBodyAdvice implements RequestBodyAdvice {
/** 此处如果返回false , 则不执行当前Advice的业务 */
@Override
public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
// return methodParameter.getMethod().isAnnotationPresent(XXApiReq.class);
return false;
}
/**
* @title 读取参数前执行
* @description 在此做些编码 / 解密 / 封装参数为对象的操作
*
* */
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
return new XHttpInputMessage(inputMessage, "UTF-8");
}
/**
* @title 读取参数后执行
* @author Xingbz
*/
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return inputMessage;
}
/**
* @title 无请求时的处理
*/
@Override
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
}
//这里实现了HttpInputMessage 封装一个自己的HttpInputMessage
class XHttpInputMessage implements HttpInputMessage {
private HttpHeaders headers;
private InputStream body;
public XHttpInputMessage(HttpInputMessage httpInputMessage, String encode) throws IOException {
this.headers = httpInputMessage.getHeaders();
this.body = encode(httpInputMessage.getBody(), encode);
}
private InputStream encode(InputStream body, String encode) {
//省略对流进行编码的操作
return body;
}
@Override
public InputStream getBody() {
return body;
}
@Override
public HttpHeaders getHeaders() {
return null;
}
}
Spring默认提供了接口的抽象实现类RequestBodyAdviceAdapter , 我们可以继承这个类按需实现 , 让代码更简洁一点
public abstract class RequestBodyAdviceAdapter implements RequestBodyAdvice {
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType)
throws IOException {
return inputMessage;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
@Override
@Nullable
public Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage,
MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}
}
Redis-设置Key的过期时间及相关策略
Redis-设置Key的过期时间及相关策略
SpringBoot自定义配置
SpringBoot中级教程之SpringBoot自定义配置(十一)
Jackson 树模型(tree model)
使用 Jackson 树模型(tree model) API 处理 JSON
在Jackson中使用树模型节点JsonNode
SpringMVC之RequestContextHolder分析
SpringMVC之RequestContextHolder分析
Java用户关闭页面,清除用户Session【服务端】
Java用户关闭页面,清除用户Session【服务端】
SpringBoot使用@ConstructorBinding注解进行配置属性绑定
SpringBoot使用@ConstructorBinding注解进行配置属性绑定
ComponentScan加载主配置类上会覆盖默认的包扫描路径
SpringBoot默认包扫描机制及使用@ComponentScan指定扫描路径
加载其他配置类上不会覆盖