Spring3.0中的AOP注解配置
来源:互联网 发布:淘宝上几千的紫砂壶 编辑:程序博客网 时间:2024/04/28 00:56
转自:http://zywang.iteye.com/blog/974226
使用@AspectJ标签
- 在配置文件中添加<aop:aspectj-autoproxy/>注解
- 创建一个Java文件,使用@Aspect注解修饰该类
- 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式
说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰
示例:
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- 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.springframework.stereotype.Component;
- /**
- * 基于注解的AOP日志示例
- * @author ZYWANG 2011-3-24
- */
- @Component
- @Aspect
- public class AopLog {
- //方法执行前调用
- @Before("execution (* com.zywang.services.impl.*.*(..))")
- public void before() {
- System.out.println("before");
- }
- //方法执行后调用
- @After("execution (* com.zywang.services.impl.*.*(..))")
- public void after() {
- System.out.println("after");
- }
- //方法执行的前后调用
- @Around("execution (* com.zywang.services.impl.*.*(..))")
- public Object around(ProceedingJoinPoint point) throws Throwable{
- System.out.println("begin around");
- Object object = point.proceed();
- System.out.println("end around");
- return object;
- }
- //方法运行出现异常时调用
- @AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")
- public void afterThrowing(Exception ex){
- System.out.println("afterThrowing");
- System.out.println(ex);
- }
- }
上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:
- import org.aspectj.lang.ProceedingJoinPoint;
- 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;
- /**
- * 基于注解的AOP日志示例
- * @author ZYWANG 2011-3-24
- */
- @Component
- @Aspect
- public class AopLog {
- @Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")
- public void pointcut(){}
- //方法执行前调用
- @Before("pointcut()")
- public void before() {
- System.out.println("before");
- }
- //方法执行的前后调用
- @Around("pointcut()")
- public Object around(ProceedingJoinPoint point) throws Throwable{
- System.out.println("begin around");
- Object object = point.proceed();
- System.out.println("end around");
- return object;
- }
- }
- Spring3.0中的AOP注解配置
- Spring3.0中的AOP配置方法
- Spring3.0中的AOP配置方法
- Spring3.0中的AOP配置方法
- spring3-基于注解的AOP
- Spring3.0配置AOP事物管理
- Spring3.0中的AOP(一)
- Spring3.0中的AOP(二)
- 使用注解配置spring中的aop
- Spring3注解零配置
- spring3.x注解配置
- Spring3注解零配置
- Spring3注解零配置
- Spring3注解零配置
- Spring3中的@value注解
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- Spring3核心技术之AOP配置
- Spring3核心技术之AOP配置
- 等待子进程
- 一天中的48小时
- 刷机后手机上不了网,设置APN
- IP地址分类
- 程序员眼里的女人O(∩_∩)O哈!
- Spring3.0中的AOP注解配置
- 基于Ubuntu12.04的Android内核源码下载介绍
- 关于连接 VirtualBox NAT 下的 MySQL 服务
- OpenGL坐标系
- 信号
- js各种验证文本框输入格式
- Hadoop源码浅析——Job提交相关
- 随笔二十五:IA32 机器代码中一些对 C 程序员隐藏的处理器状态可见
- ubuntu10.10 eclipse编译hadoop1.0.X