CommonIntercptor.java
/**
*
*/
package org.spring.aop.intercept;
/**
* @author zhangcp
*
*/
public class CommonIntercptor{
public void dobeforeMethod() {
}
public void doafterMethod() {
}
public void doafterReturningMethod() {
}
public void doafterThrowingrMethod() {
}
public void doaroundMethod() {
}
}
applicatonContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <!-- 启用aop自动代理 --> <aop:aspectj-autoproxy/> <!-- 注入切面拦截器 --> <bean id="commonIntercptor" class="org.spring.aop.intercept.CommonIntercptor"/> <aop:config> <!-- 定义切面 --> <aop:aspect id="aspect" ref="commonIntercptor"> <!-- 定义切入点 --> <aop:pointcut expression="'execution(* com.xyz.myapp.service.*.*(..))" id="pointcut"/> <!-- 切入点表达式的解释 1 * com.xyz.myapp.service.*.*(..) 表示拦截com.xyz.myapp.service包下所有类的所有方法,并且不管参数是多少和参数是什么类型,所有返回类型的方法 2 !void com.xyz.myapp.service.*.*(..) 表示拦截com.xyz.myapp.service包下所有类的所有方法,并且不管参数是多少和参数是什么类型,只有返回类型不是void类型的方法 3 !void com.xyz.myapp.service..*.*(..) 表示拦截com.xyz.myapp.service包下所有类和所有子包下的所有类的所有方法,并且不管参数是多少和参数是什么类型,只有返回类型不是void类型的方法 4 * com.xyz.myapp.service.*.*(java.lang.String,..) 表示拦截com.xyz.myapp.service包下所有类的所有方法,并且第一个参数是String类型,不管后面参数是多少和后面参数的类型,所有返回类型的方法 5 * com.xyz.myapp.service.UserService.*(..) 表示拦截com.xyz.myapp.service.UserService类的所有方法,并且不管参数是多少和参数是什么类型,所有返回类型的方法 --> <aop:before method="dobeforeMethod" pointcut="pointcut"/><!-- 前置通知 --> <aop:after method="doafterMethod" pointcut="pointcut"/><!-- 最终通知 --> <aop:after-returning method="doafterReturningMethod" pointcut="pointcut"/><!-- 后置通知 --> <aop:after-throwing method="doafterThrowingrMethod" pointcut="pointcut"/><!-- 例外通知 --> <aop:around method="doaroundMethod" pointcut="pointcut"/><!-- 环绕通知 --> </aop:aspect> </aop:config> </beans>