spring笔记

spring属性占位

https://www.baeldung.com/properties-with-spring

spring 手动aop

入口举例:MethodValidationPostProcessor

spring 工具类或接口

名称 用途
Aware/BeanNameAware/BeanClassLoaderAware/BeanFactoryAware 实现BeanFactory等的注入
BeanFactoryPostProcessor 容器的后置处理
BeanPostProcessor/InstantiationAwareBeanPostProcessor bean的后置处理
InitializingBean bean属性设置之后的回调
Lifecycle/SmartLifecycle/LifecycleProcessor 生命周期事件回调
SmartInitializingSingleton 单例初始化后回调
ApplicationListener 容器事件监听
ResolvableType 类信息解析,包括泛型
Ordered 处理顺序
StringUtils 字符串处理类
TestService()
BeanFactoryAware-setBeanFactory-
InitializingBean-afterPropertiesSet-
BeanFactoryPostProcessor-postProcessBeanFactory-
BeanPostProcessor-postProcessBeforeInitialization-com.fan.service.AbstractService@42dafa95-parentService
InitializingBean-afterPropertiesSet-
BeanPostProcessor-postProcessAfterInitialization-com.fan.service.AbstractService@42dafa95-parentService
UserBean()
BeanPostProcessor-postProcessBeforeInitialization-UserBean{userName='null'}-userBean
BeanPostProcessor-postProcessAfterInitialization-UserBean{userName='null'}-userBean
SmartInitializingSingleton-afterSingletonsInstantiated-
SmartLifecycle-isAutoStartup-
SmartLifecycle-getPhase-
SmartLifecycle-isRunning=
SmartLifecycle-start
Hello world!
AbstractService-100

SmartLifecycle-getPhase-
SmartLifecycle-isRunning=
image.png

Spring如何获取方法参数名

Spring 中 java 获取方法参数名称

获取类的泛型信息

ResolvableType

ApplicationRunner&CommandRunner

相同点:

都是在spring容器启动成功后调用执行

不同点:

方法的参数不同,主要区别在于命令行参数的获取方式上。applicationrunner更适合key=value格式,commandrunner则比较适合value的格式

Spring Mvc之定制RequestMappingHandlerMapping

resttemplate请求方式

  • 直接请求参数,一般对应@requestparam
    restTemplate.getForObject
  • 用body+contenttype,一般对应@requestbody
    restTemplate.exchange

数据校验

[参考]
https://blog.csdn.net/u013815546/article/details/77248003
https://www.jb51.net/article/143472.htm

自动校验@Validated(通过BindingResult bindingResult返回校验结果)
手动校验Validator(注意包是javax.validation.Validator)

public class Person {
@NotNull(message = "性别不能为空")
private Gender gender;
@Min(10)
private Integer age;
...
}
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
Person person = new Person();
person.setGender(Gender.Man);
validator.validate(person);

支持分组概念哦

你可能感兴趣的:(spring笔记)