spring4中文技術(shù)手冊_第1頁
spring4中文技術(shù)手冊_第2頁
spring4中文技術(shù)手冊_第3頁
spring4中文技術(shù)手冊_第4頁
spring4中文技術(shù)手冊_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

spring4中文技術(shù)手冊在《靜態(tài)代理和動態(tài)代理》中提到了面對方面編程,重要就是基于動態(tài)代理。單獨抽象出非業(yè)務(wù)的功效,服務(wù)于某些業(yè)務(wù)辦法。Spring提供了四種很實用的Advice,分別為:BeforeAdvice,AfterReturningAdvice,AroundAdvice,AfterthrowingAdvice。都是辦法級別的,就是在某個辦法執(zhí)行前后插入某些非業(yè)務(wù)的操作,如打日志或者判斷權(quán)限等。對于這四種advice的實現(xiàn),spring都提供了三種辦法,分別為基于接口、基于xml和基于annotation(注釋)。BeforeAdvice會在目的對象的辦法執(zhí)行之前被調(diào)用;AfterAdvice會在目的辦法執(zhí)行之后被調(diào)用;AroundAdvice則能夠在目的辦法執(zhí)行前后同時加上有關(guān)服務(wù);ThrowAdvice是在異常發(fā)生后執(zhí)行某些操作。1.基于接口的Advice這個就需要自定義的Aspect實現(xiàn)Spring接口。BeforeAdvice需要實現(xiàn)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實現(xiàn)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需要實現(xiàn)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()辦法中調(diào)用MethodIceed(),將執(zhí)行傳給下一種Interceptor,最后執(zhí)行目的辦法。在proceed()辦法前后加操作,達成Aroudadvice的作用。在Aspect定義好后,就需要在bean定義文獻中進行配備,通過org.springframework.aop.framework.ProxyFactoryBean的配備,指出接口、目的類和Aspect。以下:<bean

id="helloProxy"

class="org.springframework.aop.framework.ProxyFactoryBean">

<!--

業(yè)務(wù)接口

-->

<property

name="proxyInterfaces"

value="spring.advice.IHello"/>

<!--

實現(xiàn)該接口的目的類

-->

<property

name="target"

ref="helloSpeaker"/>

<!--

要應(yīng)用的解釋器,即實現(xiàn)非業(yè)務(wù)操作的Aspect

-->

<property

name="interceptorNames">

<list>

<value>throwAdvice</value>

<!--

還能夠繼續(xù)加,如

<value>logBeforeAdvice</value>

-->

</list>

</property>

</bean>

補充下,其中的目的類、解釋器(Aspect)都要在Bean定義文獻中先進行定義,然后才能夠引用的。我們使用時,代碼以下:IHello

helloProxy=(IHello)context.getBean("helloProxy");

helloProxy.hello();

在Bean定義文獻中配備的Aspect就會在適宜的joinPoint應(yīng)用到目的辦法上。補:IHello接口中有hello()辦法,HelloSpeaker類實現(xiàn)了IHello接口。以免糊涂,給個實例以下,功效是在hello()辦法前后加入日志統(tǒng)計。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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論