spring-boot-starter包,实现自定义注解开关(如Enablexxx)
2019-09-19 17:58
1406 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_36382225/article/details/101030722
前提
A项目依赖B项目,B项目配置spring注解,A项目如果直接使用该类,会报错。
- 使用@import,一般用于B项目的配置类
- 使用本文方式,一般用于无耦合的注解支持
普通注入调用
读取resource/META-INF/spring.factories,如果KEY是org.springframework.boot.autoconfigure.EnableAutoConfiguration,value写需要注入的类,可以加载多个,以逗号分隔。
这是因为在启动类注解@SpringBootApplication
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {
追踪@EnableAutoConfiguration后
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {
继续追踪@Import(AutoConfigurationImportSelector.class),进入AutoConfigurationImportSelector类
深入跟踪后会发现,项目启动后会去查找spring.factories,并根据上文说的key来注入到spring中,在A项目可以调用B项目注入的类,这个类可以是普通类,也可以是加了@Configuration的配置项类
自定义注解EnableXXX
关键接口org.springframework.context.annotation.ImportSelector;
- 自定义注解
在import中,指定自定义Selector
重写的方法,返回值作为spring注入的对象,这里需要注入MySerivice
在A项目依赖B项目后,如果不加@EnableAnn注解,直接注入MyService类,启动报错,加了注解之后启动正常
项目使用
我们使用自定义@enablexxx开启注解支持后,使用AOP针对@businessAnn进行切面操作
- 创建自定义注解支持
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(MySelector.class) public @interface EnableAnn { }
- 创建业务拦截注解
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface BusinessAnn { }
- 创建切面类
@Aspect @Order(-1) // 保证该AOP在@Transactional之前执行 public class LogAnnotationAspect { @Around("@annotation(ds)") public Object logSave(ProceedingJoinPoint joinPoint, BusinessAnn ds) throws Throwable { Object[] args = joinPoint.getArgs();// 参数值 Object result = joinPoint.proceed(); return result; } }
- 创建Selector类,并在EnableAnn类中Import
public class MySelector implements ImportSelector{ public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[] {"com.lee.jrlee27_2.config.MyConfig"}; } }
- 创建配置加载类,注入到依赖项目的spring中(如果不加Aspect类注入,AOP不会生效)
@Configuration public class MyConfig { @Bean public LogAnnotationAspect getAspect() { LogAnnotationAspect l = new LogAnnotationAspect(); return l; } }
- 主项目引用测试
@RestController @RefreshScope @EnableAnn // 开启注解支持 public class Controller extends BaseController { @RequestMapping("businessAnn") @BusinessAnn public void businessAnn() { System.out.println("businessAnn"); } }
在加了@BusinessAnn的方法,会进行AOP拦截
相关文章推荐
- springboot 自定义注解+AOP 实现日志记录
- springboot+aop+自定义注解,实现多数据源切换(有坑版)
- springboot 自定义注解 实现记录制定日志
- spring boot自定义@EnableXXX注解
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
- spring boot 自定义starter的实现教程
- Spring Boot 自动配置理解 以及实现自定义Starter
- SpringBoot自定义注解作为starter引入pom
- SpringBoot中自定义注解实现控制器访问次数限制实例
- SpringBoot使用自定义注解+拦截器 实现日志记录
- 使用SpringBoot通过自定义注解+AOP+全局异常处理实现参数统一非空校验
- Spring Boot中自定义注解结合AOP实现主备库切换问题
- Spring Boot中自定义注解+AOP实现主备库切换
- SpringBoot 通过自定义注解实现AOP切面编程实例
- spring boot---WebFilter注解 实现自定义登录过滤器
- SpringBoot 自定义注解 + AOP 实现必填参数非空校验、接口传入参数和应答数据打印、方法耗时统计
- SpringBoot中自定义注解实现控制器访问次数限制
- SpringBoot使用自定义注解实现权限拦截的示例
- Spring Boot 实现 自定义Starter
- 使用Spring Boot的AOP处理自定义注解