版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
spring4中文技术手册在《静态代理和动态代理》中提到了面对方面编程,重要就是基于动态代理。单独抽象出非业务的功效,服务于某些业务办法。Spring提供了四种很实用的Advice,分别为:BeforeAdvice,AfterReturningAdvice,AroundAdvice,AfterthrowingAdvice。都是办法级别的,就是在某个办法执行前后插入某些非业务的操作,如打日志或者判断权限等。对于这四种advice的实现,spring都提供了三种办法,分别为基于接口、基于xml和基于annotation(注释)。BeforeAdvice会在目的对象的办法执行之前被调用;AfterAdvice会在目的办法执行之后被调用;AroundAdvice则能够在目的办法执行前后同时加上有关服务;ThrowAdvice是在异常发生后执行某些操作。1.基于接口的Advice这个就需要自定义的Aspect实现Spring接口。BeforeAdvice需要实现org.springframework.aop.MethodBeforeAdvice接口:/**
*
Advice
invoked
before
a
method
is
invoked.
Such
advices
cannot
*
prevent
the
method
call
proceeding,
unless
they
throw
a
Throwable.
*/
public
interface
MethodBeforeAdvice
extends
BeforeAdvice
{
/**
*
Callback
before
a
given
method
is
invoked.
*
@param
method
method
being
invoked
*
@param
args
arguments
to
the
method
*
@param
target
target
of
the
method
invocation.
May
be
<code>null</code>.
*
@throws
Throwable
if
this
object
wishes
to
abort
the
call.
*
Any
exception
thrown
will
be
returned
to
the
caller
if
it's
*
allowed
by
the
method
signature.
Otherwise
the
exception
*
will
be
wrapped
as
a
runtime
exception.
*/
void
before(Method
method,
Object[]
args,
Object
target)
throws
Throwable;
}
AfterAdvice实现org.springframework.aop.AfterReturningAdvice接口:/**
*
After
returning
advice
is
invoked
only
on
normal
method
return,
not
if
an
*
exception
is
thrown.
Such
advice
can
see
the
return
value,
but
cannot
change
it.
*/
public
interface
AfterReturningAdvice
extends
AfterAdvice
{
/**
*
Callback
after
a
given
method
successfully
returned.
*
@param
returnValue
the
value
returned
by
the
method,
if
any
*
@param
method
method
being
invoked
*
@param
args
arguments
to
the
method
*
@param
target
target
of
the
method
invocation.
May
be
<code>null</code>.
*
@throws
Throwable
if
this
object
wishes
to
abort
the
call.
*
Any
exception
thrown
will
be
returned
to
the
caller
if
it's
*
allowed
by
the
method
signature.
Otherwise
the
exception
*
will
be
wrapped
as
a
runtime
exception.
*/
void
afterReturning(Object
returnValue,
Method
method,
Object[]
args,
Object
target)
throws
Throwable;
}
AroundAdvice需要实现ercept.MethodInterceptor接口:/**
*
<p>The
user
should
implement
the
{@link
#invoke(MethodInvocation)}
*
method
to
modify
the
original
behavior.
E.g.
the
following
class
*
implements
a
tracing
interceptor
(traces
all
the
calls
on
the
*
intercepted
method(s)):
*
*
<pre
class=code>
*
class
TracingInterceptor
implements
MethodInterceptor
{
*
Object
invoke(MethodInvocation
i)
throws
Throwable
{
*
System.out.println("method
"+i.getMethod()+"
is
called
on
"+
*
i.getThis()+"
with
args
"+i.getArguments());
*
Object
ret=ceed();
*
System.out.println("method
"+i.getMethod()+"
returns
"+ret);
*
return
ret;
*
}
*
}
*
</pre>
*/
public
interface
MethodInterceptor
extends
Interceptor
{
/**
*
Implement
this
method
to
perform
extra
treatments
before
and
*
after
the
invocation.
Polite
implementations
would
certainly
*
like
to
invoke
{@link
Joinpoint#proceed()}.
*
*
@param
invocation
the
method
invocation
joinpoint
*
@return
the
result
of
the
call
to
{@link
*
Joinpoint#proceed()},
might
be
intercepted
by
the
*
interceptor.
*
*
@throws
Throwable
if
the
interceptors
or
the
*
target-object
throws
an
exception.
*/
Object
invoke(MethodInvocation
invocation)
throws
Throwable;
}
类前面的注释阐明了该办法的使用,就是要在invoke()办法中调用MethodIceed(),将执行传给下一种Interceptor,最后执行目的办法。在proceed()办法前后加操作,达成Aroudadvice的作用。在Aspect定义好后,就需要在bean定义文献中进行配备,通过org.springframework.aop.framework.ProxyFactoryBean的配备,指出接口、目的类和Aspect。以下:<bean
id="helloProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!--
业务接口
-->
<property
name="proxyInterfaces"
value="spring.advice.IHello"/>
<!--
实现该接口的目的类
-->
<property
name="target"
ref="helloSpeaker"/>
<!--
要应用的解释器,即实现非业务操作的Aspect
-->
<property
name="interceptorNames">
<list>
<value>throwAdvice</value>
<!--
还能够继续加,如
<value>logBeforeAdvice</value>
-->
</list>
</property>
</bean>
补充下,其中的目的类、解释器(Aspect)都要在Bean定义文献中先进行定义,然后才能够引用的。我们使用时,代码以下:IHello
helloProxy=(IHello)context.getBean("helloProxy");
helloProxy.hello();
在Bean定义文献中配备的Aspect就会在适宜的joinPoint应用到目的办法上。补:IHello接口中有hello()办法,HelloSpeaker类实现了IHello接口。以免糊涂,给个实例以下,功效是在hello()办法前后加入日志统计。public
class
AroundAdvice
implements
MethodInterceptor{
private
Logger
logger=
Logger.getLogger(this.getClass().getName());
@Override
public
Object
invoke(MethodInvocation
methodInvocation)
throws
Throwable
{
logger.log(Level.INFO,
"method
starts..."+methodInvocation.getMethod());
Object
result=methodIceed();
logger.log(Level.INFO,"method
ends..."+methodInvocation.getMethod());
return
result;
}
}
配备:<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="://./schema/beans"
xmlns:xsi="://.//XMLSchema-instance"
xsi:schemaLocation="://./schema/beans
://./schema/beans/spring-beans-3.0.xsd">
<bean
id="helloSpeaker"
class="spring.advice.HelloSpeaker"/>
<bean
id="aroundAdvice"
class="spring.advice.AroundAdvice"/>
<bean
id="helloProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property
name="proxyInterfaces"
value="spring.advice.IHello"
/>
<property
name="target"
r
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 插花摆摊活动方案策划(3篇)
- 施工方案项目部人员(3篇)
- 机房下送风施工方案(3篇)
- 楼道底部刷漆施工方案(3篇)
- 池塘清淤护坡施工方案(3篇)
- 活动食物策划方案范文(3篇)
- 滑板冲浪提供营销方案(3篇)
- 盖板勾缝施工方案(3篇)
- 移动入户活动方案策划(3篇)
- 纸包鱼店面营销方案(3篇)
- 2026石家庄新天智慧能源有限公司招聘44人考试备考试题及答案解析
- 2026春季江西铜业集团有限公司贵溪冶炼厂校园招聘变更20人笔试备考试题及答案解析
- 2026年全民营养周营养餐桌家庭健康宣传课件
- 算电协同发展契机 (课件)
- 2026年四川省成都市网格员招聘考试参考试题及答案解析
- ISO140012026标准解读文件
- 机关工会财务审批制度
- 八年级义务教育劳动国测模拟试题
- 2026年职工职业技能竞赛(泵站运行工赛项)参考试指导题库(含答案)
- 2026年如何制定有效的设备维护计划
- 招商运营部制度汇编范本
评论
0/150
提交评论