版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2027年备考会计基础 【零基础学员必练】通关题库(附带答案)
- 事业单位统计岗面试全真模拟试卷及参考答案2026
- 2026年湖南工业职业技术学院高职单招笔试职业适应性测验历年参考题库含答案解析
- 2026年湖南中医药高等专科学校高职单招笔试职业技能测验历年参考题库含答案解析
- 2026年河南检察职业学院高职单招笔试化学历年参考题库含答案解析
- 2026年河北地质职工大学高职单招笔试职业适应性测验历年参考题库含答案解析
- 2026年江西软件职业技术大学高职单招笔试英语历年参考题库含答案解析
- 药剂员考试题库及参考答案
- 药学不良事件上报员(用药差错)考试模拟试卷(含答案)
- 营养缺乏病防治试题库及答案
- 2026年上海企业负责人a证模拟试卷目带答案
- 浙江宁波市2026-2027学年第一学期模拟考试高三语文试卷(含答案)
- 2025年新疆公安辅警招聘知识考试题库及答案
- 2026年江苏省职业院校技能大赛-高职组(药学技能赛项)考试题库(带答案)
- 2026年译林版五年级英语上册期中测试卷(含答案)-中等难度第10套-含答案解析
- 2026年《口腔颌面部外伤诊疗指南》
- 2026年新修订《中国共产党发展党员工作细则》逐条全文精讲(权威完整版)
- 2026上海中考作文《如愿以偿之后》深度解析 + 6篇满分范文
- 墙面抹灰空鼓开裂修补方案
- 林木采伐专项施工方案
- 青岛水务集团招聘74人笔试试题
评论
0/150
提交评论