您好,登錄后才能下訂單哦!
這篇文章跟大家分析一下“Spring api如何手動創(chuàng)建代理對象ProxyFactory”。內(nèi)容詳細(xì)易懂,對“Spring api如何手動創(chuàng)建代理對象ProxyFactory”感興趣的朋友可以跟著小編的思路慢慢深入來閱讀一下,希望閱讀后能夠?qū)Υ蠹矣兴鶐椭?。下面跟著小編一起深入學(xué)習(xí)“Spring api如何手動創(chuàng)建代理對象ProxyFactory”的知識吧。
可以通過注解的方式來自定義代理對象的創(chuàng)建,同時也可以通過 SpringAPI,手動編程的方式來創(chuàng)建代理對象。
幾個重要的API:
ProxyFactory
MethodInterceptor
Advice
AfterReturningAdvice
MethodBeforeAdvice
import java.lang.reflect.Method;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import cn.hessian.service.HelloWorldService;
import cn.hessian.service.impl.HelloWorldServiceImpl2;
/**
* @author beijing
* 2013-4-2
*/
public class SpringProgramicProxyDemo {
@Test
public void test(){
//代理對象需要的實(shí)現(xiàn)的接口
Class[] interfaces=new Class[]{HelloWorldService.class};
//利用spring的API,創(chuàng)建代理工廠
ProxyFactory proxyFactory=new ProxyFactory(interfaces);
//設(shè)置目標(biāo)對象
proxyFactory.setTarget(new HelloWorldServiceImpl());
/**
* Set whether proxies created by this configuration should be prevented from being cast to Advised to query proxy status.
Default is "false", meaning that any AOP proxy can be cast to Advised.
* */
proxyFactory.setOpaque(true);
//添加方法前置通知
proxyFactory.addAdvice(new MethodBeforeAdvice() {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("1---在方法調(diào)用之前攔截");
}
});
//可以添加多個方法前置或者后置通知
proxyFactory.addAdvice(new MethodBeforeAdvice() {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("2---在方法調(diào)用之前攔截");
}
});
//可以添加多個方法前置或者后置通知
proxyFactory.addAdvice(new AfterReturningAdvice() {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("方法完成之后調(diào)用的方法---1");
}
});
//可以添加多個方法前置或者后置通知
proxyFactory.addAdvice(new AfterReturningAdvice() {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("方法完成之后調(diào)用的方法---2");
}
});
//對于環(huán)繞通知只能添加一個,多添加也是沒有用的,spring會選第一個advice,請看結(jié)果
proxyFactory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("1---環(huán)繞通知");
Object[] params=invocation.getArguments();
Method method=invocation.getMethod();
Object target=invocation.getThis();
Object bytes=method.invoke(target, params);
byte[] result=(byte[]) bytes;
System.out.println("1---環(huán)繞通知生成的結(jié)果:"+new String(result));
return "北京生活圈".getBytes();
}
});
//對于環(huán)繞通知只能添加一個,多添加也是沒有用的,spring會選第一個advice,請看結(jié)果
proxyFactory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("2---環(huán)繞通知");
Object[] params=invocation.getArguments();
Method method=invocation.getMethod();
Object target=invocation.getThis();
Object bytes=method.invoke(target, params);
byte[] result=(byte[]) bytes;
System.out.println("2---環(huán)繞通知生成的結(jié)果:"+new String(result));
return bytes;
}
});
Object proxy=proxyFactory.getProxy(proxyFactory.getClass().getClassLoader());
Class[] inters=proxy.getClass().getInterfaces();
for(Class str: inters ){
System.out.println(str.getSimpleName());
}
HelloWorldService helloWorldService=(HelloWorldService)proxy;
System.out.println(new String(helloWorldService.sayHelloWorld("北京")));
}
}
關(guān)于Spring api如何手動創(chuàng)建代理對象ProxyFactory就分享到這里啦,希望上述內(nèi)容能夠讓大家有所提升。如果想要學(xué)習(xí)更多知識,請大家多多留意小編的更新。謝謝大家關(guān)注一下億速云網(wǎng)站!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。