首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Spring AOP 中 advice 的四类别型 before after throwing advice around

2013-03-22 
SpringAOP 中 advice 的四种类型beforeafterthrowing advicearoundSpringAOP(Aspect-oriented programming

Spring AOP 中 advice 的四种类型 before after throwing advice around

Spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截正在执行的方法,在方法执行的前或者后增加额外的功能和处理。


在Spring AOP中支持4中类型的通知:

1:before advice 在方法执行前执行。

2:after  returning  advice 在方法执行后返回一个结果后执行。

3:after  throwing advice 在方法执行过程中抛出异常的时候执行。

4:Around  advice 在方法执行前后和抛出异常时执行,相当于综合了以上三种通知。


下面是一个简单的AOP  advice 的例子:

 首先给出一个简单的Spring 注入的例子,

定义一个Book类:

三月 20, 2013 3:02:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 15:02:19 CST 2013]; root of context hierarchy三月 20, 2013 3:02:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [resource/aop.xml]三月 20, 2013 3:02:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e29127: defining beans [book,aroundMethod,bookProxy]; root of factory hierarchy---------------------method  name:printNamemethod  arguments[]Around  method : before Book name Effective javaAround method : after ---------------------method  name:printUrlmethod  arguments[]Around  method : before Book URL www.google.cnAround method : after ----------------------method  name:printThrowExceptionmethod  arguments[]Around  method : before Around method : throw  an  exception 

around  advice得到实现。 over



热点排行