文章内容
在spring boot中使用spring-boot-starter-aop对自定义注解的支持,本质上是spring aop对自定义注解的支持。
spring-boot-starter-aop自定义注解步骤如下:
1、编写自定义注解类
1 2 3 4 5 | @Documented @Target ({ElementType.PARAMETER, ElementType.METHOD}) @Retention (RetentionPolicy.RUNTIME) public @interface ULog { } |
2、编写注解类切面
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | @Aspect @Component public class HLogAspect { @Pointcut ( "@annotation(com.yql.service.ULog)" ) public void annotationPointCut() { } @Before ( "annotationPointCut()" ) public void before(JoinPoint joinPoint){ MethodSignature sign = (MethodSignature)joinPoint.getSignature(); Method method = sign.getMethod(); System.out.print( "接受方法:" +method.getName()+ " 前置日志" ); } } |
3、在spring boot的Application启动类中使用@EnableAspectJAutoProxy注解,添加对Aspectj支持
1 2 3 4 5 6 7 8 9 | @SpringBootApplication //开启spring对Aspectj的支持 @EnableAspectJAutoProxy public class AopApplication { public static void main(String[] args) { SpringApplication.run(AopApplication. class , args); } } |
4、调用自定义注解
1 2 3 4 5 6 7 8 9 | @RestController public class TestController { @RequestMapping (value= "/" ) @ULog public String test(){ return "hello world" ; } } |
5、pom文件需要添加一下依赖
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | < dependencies > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-aop</ artifactId > </ dependency > </ dependencies > |