设为首页 加入收藏

TOP

Spring AOP的注解实例(二)
2015-11-12 23:05:55 来源: 作者: 【 】 浏览:24
Tags:Spring AOP 注解 实例
Point + "\tUse time : " + (end - start) + " ms with exception : " + e.getMessage());
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? //前置通知等可以没有JoinPoint参数
? ? @Before("aspect()")
? ? public void doBefore(JoinPoint joinPoint) {
? ? ? ? System.out.println("==========执行前置通知===============");
? ? ? ? if(log.isInfoEnabled()){
? ? ? ? ? ? log.info("before " + joinPoint);
? ? ? ? }
? ? }? ? ?
? ? //配置后置通知,使用在方法aspect()上注册的切入点
? ? @After("aspect()")
? ? public void doAfter(JoinPoint joinPoint) {
? ? ? ? System.out.println("===========执行后置通知==============");
? ? ? ? if(log.isInfoEnabled()){
? ? ? ? ? ? log.info("after " + joinPoint);
? ? ? ? }
? ? }?
? ? //配置后置返回通知,使用在方法aspect()上注册的切入点
? ? @AfterReturning("aspect()")
? ? public void afterReturn(JoinPoint joinPoint){


? ? ? ? ? ? System.out.println("===========执行后置返回通知==============");
? ? ? ? ? ? if(log.isInfoEnabled()){
? ? ? ? ? ? ? ? log.info("afterReturn " + joinPoint);
? ? ? ? ? ? }
? ? }?
? ? //配置抛出异常后通知,使用在方法aspect()上注册的切入点
? ? @AfterThrowing(pointcut="aspect()", throwing="ex")
? ? public void afterThrow(JoinPoint joinPoint, Exception ex){
? ? ? ? ? ? if(log.isInfoEnabled()){
? ? ? ? ? ? ? ? log.info("afterThrow " + joinPoint + "\t" + ex.getMessage());
? ? ? ? ? ? }
? ? }? ?
}
测试类



? ? @Test
? ? public void testAOP1(){
? ? ? ? //启动Spring容器? ? ? ?
? ? ? ? ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-mvc.xml","classpath:applicationContext-dataSource.xml"});
? ? ? ? UserService userService = (UserService) ctx.getBean("userService");
? ? ? ? userService.addUser("zhangsan", "123456");
? ? }


效果图:



OK!!简单的spring AOP实现了,在这基础上可以做下AOP的简单日志管理了。我的习惯是在后置通知里添加日志的逻辑代码。但这这种方式还不是最灵活的,下一篇做下spring AOP 的自定义注解实现日志管理。


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring AOP的两种代理 下一篇Spring AOP自定义注解方式实现日..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: