数据源
动态数据源
- application.properties中配置多个数据源
- 实现ImportBeanDefinitionRegister,EnviromentAware接口,初始化并出册数据源,引入SpringBootApplication注解的类中(@Import('class'))
- 创建包含ThreadLocal的类,供动态选择数据源
- 实现AbstractRoutingDataSource类,引用3中的类,决定数据源
可参考
https://www.liaoxuefeng.com/article/00151054582348974482c20f7d8431ead5bc32b30354705000
自定义注解
指定target,方式@Target(java.lang.annotaion.ElementType.*)
指定retention,方式@Retention(java.lang.annotaion.RetentionPolicy.*)
signature返回类型@interface
1通过handler绑定业务逻辑
- 通过一个实现HandlerMethodArgumentResolver的类将该注解与具体业务逻辑绑定
- 后将其在WebMvcConfigurerAdapter的实现类中通过addArgumentResolvers添加实现了HandlerMethodArgumentResolver的类
- 可参考
- https://docs.spring.io/spring/docs/5.1.0.BUILD-SNAPSHOT/javadoc-api/
- 实现不同类型的接口或抽象类绑定不同的业务逻辑
2通过aop
- @Around("@annotation(_the annotation name_)")
- 使用aop时,需要注意2点
- 1. 使用@Aspect和@Component注解创建aop类,使用@EnableAspectJAutoProxy(proxyTargetClass=true)再SpringBootApplication类中开启aop
- 2. 切入点可以直接使用或自定义切面,格式为modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?,其中ret-type-pattern name-pattern param-pattern为必须,即方法签名必须。
filter
- 可以实现HandlerInterceptor或继承HandlerInterceptorAdapter并声明为component,可以选择重写preHandler,postHandler,afterCompletion方法
- 创建类继承WebMvcConfigurerAdapter,并声明为Configuration,重写addIntecetpors添加1中拦截器
- 可参考https://docs.spring.io/spring/docs/5.1.0.BUILD-SNAPSHOT/javadoc-api/
authentication
*.config.SecurityConfig.configure(AuthenticationManagerBuilder auth)->new StandardPasswordEncoder("");
*.config.SecurityConfig.configure(HttpSecurity http);(另外还有可以定义在方法级的注解授权,如:
@PreAuthorize("hasRole('USER')")
可参考https://docs.spring.io/spring-security/site/docs/4.1.3.RELEASE/reference/htmlsingle/#method-security-meta-annotations)