设为首页 加入收藏

TOP

Spring AOP 中 advice 的四种类型 before after throwing advice around(三)
2014-11-24 11:59:58 来源: 作者: 【 】 浏览:115
Tags:Spring AOP advice 类型 before after throwing around
xy]; root of factory hierarchy
---------------------
Before Method
--------------------
Book name Effective java
---------------------
Before Method
--------------------
Book URL www.google.cn
----------------------
Before Method
--------------------
2: after advice
在方法运行返回结果后将执行这个 afterReturning方法,创建的这个类必须实现:AfterReturningAdvice接口
[java]
package com.myapp.core.aop.advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterMethod implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
System.out.println("-------------------");
System.out.println("After method ");
}
}
xml配置文件:
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
beforeMethodBean
afterMethodBean
运行结果如下:
[plain]
三月 20, 2013 2:22:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 14:22:19 CST 2013]; root of context hierarchy
三月 20, 2013 2:22:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resource/aop.xml]
三月 20, 2013 2:22:20 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157985: defining beans [book,beforeMethodBean,afterMethodBean,bookProxy]; root of factory hierarchy
---------------------
Before Method
--------------------
Book name Effective java
-------------------
After method
---------------------
Before Method
--------------------
Book URL www.google.cn
-------------------
After method
----------------------
Before Method
--------------------
3:after throwing advice
当方法执行抛出一个异常后,会执行这个方法,创建一个类实现:ThrowsAdvice接口,创建一个afterThrowing拦截:IllegalArgumentException异常。
类如下:
[java]
package com.myapp.core.aop.advice;
import org.springframework.aop.ThrowsAdvice;
public class ThrowException implements ThrowsAdvice{
public void afterThrowing(IllegalArgumentException e) throws Throwable{
System.out.println("after Throwing Exception");
}
}
xml中配置文件如下:
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.s
首页 上一页 1 2 3 4 5 下一页 尾页 3/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇图的最短路径dijkstra算法 下一篇Java5 多线程--Semaphore实现信号..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)