spring boot 自定义注解
在此向各位说声抱歉,此文章还在编辑完善中,此文主要展示demo(感觉展示demo已经够了)
1:引入包
- gradle
compile "org.springframework.boot:spring-boot-starter-aop"
- maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2:配置自定义注解
import java.lang.annotation.ElementType
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Documented
import java.lang.annotation.Inherited
import java.lang.annotation.Retention
import java.lang.annotation.Target
@Target([ElementType.METHOD, ElementType.TYPE])
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface TestCacheAnnotation {
String key() //注解的参数,没有default就必须设置值,详见最下方注解使用
boolean needLog() default true//注解的参数,default为默认值,在不设置值得时候使用默认值
}
3:配置Aspect
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.After
import org.aspectj.lang.annotation.AfterReturning
import org.aspectj.lang.annotation.AfterThrowing
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.springframework.stereotype.Component
import org.aspectj.lang.ProceedingJoinPoint
@Aspect
@Component
class TestCacheAspect {
//
@Pointcut("@annotation(grails.annotation.test.TestCacheAnnotation)")
void cache(){}
/**
* 环绕增强,相当于MethodInterceptor
*/
@Around("cache()")
Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
println("--------------1----------------")
// MethodSignature signature = (MethodSignature)joinPoint.getSignature()
def res = joinPoint.proceed()
println("--------------2----------------")
return res
}
@Before("cache()")
void doBeforeAdvice(JoinPoint joinPoint){
println("进入方法前执行.....")
}
/**
* 处理完请求,返回内容
* @param ret
*/
@AfterReturning(returning = "ret", pointcut = "cache()")
void doAfterReturning(Object ret) {
println("方法的返回值 : " + ret)
}
/**
* 后置异常通知
*/
@AfterThrowing("cache()")
void throwss(JoinPoint joinPoint){
println("方法异常时执行.....")
}
/**
* 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
*/
@After("cache()")
void after(JoinPoint joinPoint){
println("方法最后执行.....")
}
}
3:在需要的地方(注解设定的范围)添加注解
@TestCacheAnnotation(key="1")
@RequestMapping("/test/index")
String index() {
System.out.println("--------------in----------------");
return "test";
}
grails 自定义注解
注入spring AOC的依赖包
//AOP依赖
compile 'aspectj:aspectjrt:1.5.0'
compile 'aspectj:aspectjweaver:1.5.0'
compile 'cglib:cglib:2.2'
创建annotation
import java.lang.annotation.ElementType
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Retention
import java.lang.annotation.Target
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface TestCacheAnnotation {
String key()
boolean needLog() default true
}
创建aspect
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Aspect
import org.springframework.stereotype.Component
@Component
@Aspect
class TestCacheAspect {
def chche(ProceedingJoinPoint pjp){
println("------------1--------------")
def proceed = pjp.proceed()
println("------------2--------------")
return proceed
}
}
在需要使用的地方添加注解
@TestCacheAnnotation(key="test")
注解注意
多个注解的执行顺序
使Aspect实现Ordered接口(org.springframework.core.Ordered)
@Override
int getOrder() {
return 10000
}
order越大,越优先执行
最先执行的,最后结束