AOP 基于注解和基于xml的写法
0 前期准备
加入 jar 包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
……
在 Spring 的配置文件头部加入 aop 的命名空间。
1 2 3 4 5 6 7 8 9 10
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
|
1 基于注解使用AOP
1.1 在配置文件xml中
1 2
| //打开注解支持 <context:annotation-config/>
|
1 2
| //配置自动扫描的包 <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
|
1 2
| //使 AspjectJ 注解起作用,使匹配的类自动生成动态代理对象 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
|
1.2 编写切面aspectj类:
- 一个一般的 Java 类
- 在其中添加要额外实现的功能.
- 切面必须是 IOC 中的 bean:
- 声明是一个切面: 添加 @Aspect
- 声明通知: 即额外加入功能对应的方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| @Component("logAspect") @Aspect public class LogAspect {
@Before("execution(* org.zttc.itat.spring.dao.*.add*(..))||" + "execution(* org.zttc.itat.spring.dao.*.delete*(..))||" + "execution(* org.zttc.itat.spring.dao.*.update*(..))") public void logStart(JoinPoint jp) { System.out.println(jp.getTarget()); System.out.println(jp.getSignature().getName()); Logger.info("加入日志"); }
@After("execution(* org.zttc.itat.spring.dao.*.add*(..))||" + "execution(* org.zttc.itat.spring.dao.*.delete*(..))||" + "execution(* org.zttc.itat.spring.dao.*.update*(..))") public void logEnd(JoinPoint jp) { Logger.info("方法调用结束加入日志"); }
@Around("execution(* org.zttc.itat.spring.dao.*.add*(..))||" + "execution(* org.zttc.itat.spring.dao.*.delete*(..))||" + "execution(* org.zttc.itat.spring.dao.*.update*(..))") public void logAround(ProceedingJoinPoint pjp) throws Throwable { Logger.info("开始在Around中加入日志"); pjp.proceed(); Logger.info("结束Around"); }
}
|
2 基于xml使用AOP
2.1 在配置文件xml中,基于”xml”的方式来使用 AOP
1 2
| //打开注解支持 <context:annotation-config/>
|
1 2
| //配置自动扫描的包 <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
|
1 2 3 4 5 6 7 8 9 10 11 12
| <aop:config>
<aop:aspect id="myLogAspect" ref="logAspect"> <aop:pointcut id="logPointCut" expression="execution(* org.zttc.itat.spring.dao.*.add*(..))|| execution(* org.zttc.itat.spring.dao.*.delete*(..))|| execution(* org.zttc.itat.spring.dao.*.update*(..))"/> <aop:before method="logStart" pointcut-ref="logPointCut"/> <aop:after method="logEnd" pointcut-ref="logPointCut"/> <aop:around method="logAround" pointcut-ref="logPointCut"/> </aop:aspect> </aop:config>
|
2.2 编写切面aspectj类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Component("logAspect") public class LogAspect {
public void logStart(JoinPoint jp) { System.out.println(jp.getTarget()); System.out.println(jp.getSignature().getName()); Logger.info("加入日志"); } public void logEnd(JoinPoint jp) { Logger.info("方法调用结束加入日志"); }
public void logAround(ProceedingJoinPoint pjp) throws Throwable { Logger.info("开始在Around中加入日志"); pjp.proceed(); Logger.info("结束Around"); }
}
|
AOP切面通知详解