spring3-基于注解的AOP

要点:

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

https://jira.springsource.org/browse/SPR-6992?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel

@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表达式说明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值