要点:
1.aop的概念真的很多。。。其实从使用出发无非两点:1,定义要拦截的方法,2,实现拦截后的操作方法。
2.基于注解的@Aspect需要配合bean声明来用,不然不报错,不执行。。官方doc貌似没提倒。
3. 注入的bean对象,访问其属性需要生成get/set方法, 如果直接访问也会出现空指针。
@Autowired private MemCacheService memCacheService; //报空指针 memCacheService.memcachedClient.set(); //下面方法ok memCacheService.getMemcachedClient().set()
4.极有可能导致基于注解的bean嵌套bean产生NullPointerException
https://jira.springsource.org/browse/SPR-10594
@Component @Aspect
具体做法示例代码如下:1.execution**指定拦截service包,2.@@Around说明在方法前后拦截。 详细说明可以参考附录文档
@Component @Aspect public class ServiceAdvice { Logger log = Logger.getLogger(ServiceAdvice.class); /** * 统一的函数耗时统计;返回结果打印 * @param pjp * @return * @throws Throwable */ @Around("execution(* com.xx.service.*.*(..))") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { // start stopwatch StopWatch watch = new StopWatch(pjp.toShortString()); watch.start(); Object retVal = pjp.proceed(); watch.stop(); log.info("##### return #######"+LoggerHelper.ObjectComposer(retVal)); log.info("##### StopWatch #######"+watch.shortSummary()); return retVal; } }
参考资料:
1.http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/aop.html 官方doc,永远最有用
2.http://sishuok.com/forum/blogPost/list/2472.htmlAOP 之 6.5 AspectJ切入点语法详解 最详细的aop表达式说明。