08 vim编辑
2015-07-23 10:17
281 查看
面向切面编程,代码如下:
注:任何通知方法可以将第一个参数定义为org.aspectj.lang.JoinPoint类型 (环绕通知需要定义第一个参数为ProceedingJoinPoint类型, 它是 JoinPoint 的一个子类)。
以上注意几点:
1、该类声明为@Aspect;
2、在spring配置文件中声明:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="controllerAspect"
class="com.spring.test.common.ControllorAspectj"/>
@Aspect public class ControllorAspectj { @Pointcut("within(com.spring.test.action.*)") public void doManagerActionClass() { } @Before("execution(public * * (..))") public void getUser(JoinPoint jp) throws IOException { System.out.println("任意公共方法的执行"); } @Pointcut("execution(* do*(..))") public void doMethod(){ } @Before(value="com.spring.test.common.ControllorAspectj.doMethod() && target(bean)",argNames="bean") public void beforeDo(JoinPoint jp,Object bean) throws Exception { String sourceLocation=jp.getKind(); Signature signature=jp.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); method.invoke(jp.getTarget(),jp.getArgs()); System.out.println("方法名:"+method.getName()); System.out.println("任意以do开头的方法"+sourceLocation); } @Pointcut("within(com.spring.test..*)") public void before3(){ System.out.println("test包以及他的子包"); } @AfterThrowing(pointcut="com.spring.test.common.ControllorAspectj.before3()",throwing="ex") public void afterThrows(JoinPoint jp,Throwable ex) { ex.printStackTrace(); System.out.println("test包以及他的子包抛出的异常"+ex.getMessage()); } @AfterReturning("this(com.spring.test.IBaseAction)") public void afterReturn(){ System.out.println("after return,实现了IBaseAction"); } }
注:任何通知方法可以将第一个参数定义为org.aspectj.lang.JoinPoint类型 (环绕通知需要定义第一个参数为ProceedingJoinPoint类型, 它是 JoinPoint 的一个子类)。
以上注意几点:
1、该类声明为@Aspect;
2、在spring配置文件中声明:
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="controllerAspect"
class="com.spring.test.common.ControllorAspectj"/>
相关文章推荐
- CentOS文件内容查阅命令(cat、tac、nl、more、less、head、tail、touch)
- STL set
- 种子填充找连通块 floodfill
- swift学习01-性能评测
- make 2>&1 | tee build.log 求解释
- java基础之 反射
- [leetcode] Longest Consecutive Sequence
- OI流水账(慎入)
- 使用Kotlin开发Android
- 安装多个jdk导致eclipse打不开问题解决
- codeforce(559B)
- (新)Android 汉子首字母排序
- Codeforces546C:Soldier and Cards
- jQuery选择器
- C++转让Lua
- 关于电信cdma基站nid,sid,bid的解释
- 创建数据上下文
- hdoj 2087 剪花布条
- iOS开发,最新判断是否是手机号的正则表达式
- MFC学习笔记.day01续-调试MessageBox,注册窗口,创建窗口,显示,更新,消息循环,回调函数