使用注解实现AOP,注意版本问题,使用注解报错要导入maven依赖
<dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.2</version> </dependency>
使用注解实现AOP步骤
1.xml配置文件中注意context的添加----注解
2.开始aop支持
<!-- 方式三 注解实现AOP,要在配置文件中开启自动代理--> <aop:aspectj-autoproxy/>
3.编写切面代码,并将其添加到Spring容器中
@Component @Aspect public class AnnotationAop { @Before("execution(* com.chen.service.UserServiceImpl.*(..))") public void beforeMeahod(){ System.out.println("===========使用注解实现AOP方法前=================="); } @Around("execution(* com.chen.service.UserServiceImpl.*(..))")
//环绕通知可以传递参数,ProceedingJoinPoint,使得切入点方法可以继续执行 public void Around(ProceedingJoinPoint pj) throws Throwable { System.out.println("=================包围实现AOP前==============="); pj.proceed();//执行方法 执行切点的那个方法,之情方法,才会调用上面的beforeMethod System.out.println("=================包围实现AOP前==============="); } }
4.配置文件和注解对比
------------恢复内容结束------------