1.使用maven引入依赖:
<dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.4.RELEASE</version> </dependency>
2.配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--XML的配置方式完成AOP的开发===============--> <!--配置目标类=================--> <bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/> <!--配置切面类--> <bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"/> <!--aop的相关配置=================--> <aop:config> <!--配置切入点--> <aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/> <aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/> <aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/> <aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/> <aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/> <!--配置AOP的切面--> <aop:aspect ref="myAspectXml"> <!--配置前置通知--> <aop:before method="before" pointcut-ref="pointcut1"/> <!--配置后置通知--> <aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/> <!--配置环绕通知--> <aop:around method="around" pointcut-ref="pointcut3"/> <!--配置异常抛出通知--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/> <!--配置最终通知--> <aop:after method="after" pointcut-ref="pointcut5"/> </aop:aspect> </aop:config> </beans>