1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、通过配置方式接入aop
@Aspect
@Configuration
public class CacheAop {
@Autowired
private CacheService cacheService;
@Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint){
System.err.println ("切面执行了。。。。");
System.out.println("@Before:模拟权限检查...");
String method = thisJoinPoint.getSignature().getDeclaringTypeName() +
"." + thisJoinPoint.getSignature().getName();
System.out.println("@Before:目标方法为:" +
method);
String argus = Arrays.toString(thisJoinPoint.getArgs());
System.out.println("@Before:参数为:" + argus);
System.out.println("@Before:被织入的目标对象为:" + thisJoinPoint.getTarget());
try {
Object result = cacheService.getResult(method,argus,thisJoinPoint);
return result;
} catch (Throwable e) {
e.printStackTrace ();
}
return null;
}
}
http://www.oschina.net/code/snippet_727875_49426#71856