您好,登錄后才能下訂單哦!
這篇文章跟大家分析一下“Spring api如何手動創建代理對象ProxyFactory”。內容詳細易懂,對“Spring api如何手動創建代理對象ProxyFactory”感興趣的朋友可以跟著小編的思路慢慢深入來閱讀一下,希望閱讀后能夠對大家有所幫助。下面跟著小編一起深入學習“Spring api如何手動創建代理對象ProxyFactory”的知識吧。
可以通過注解的方式來自定義代理對象的創建,同時也可以通過 SpringAPI,手動編程的方式來創建代理對象。
幾個重要的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(){
//代理對象需要的實現的接口
Class[] interfaces=new Class[]{HelloWorldService.class};
//利用spring的API,創建代理工廠
ProxyFactory proxyFactory=new ProxyFactory(interfaces);
//設置目標對象
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---在方法調用之前攔截");
}
});
//可以添加多個方法前置或者后置通知
proxyFactory.addAdvice(new MethodBeforeAdvice() {
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("2---在方法調用之前攔截");
}
});
//可以添加多個方法前置或者后置通知
proxyFactory.addAdvice(new AfterReturningAdvice() {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("方法完成之后調用的方法---1");
}
});
//可以添加多個方法前置或者后置通知
proxyFactory.addAdvice(new AfterReturningAdvice() {
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("方法完成之后調用的方法---2");
}
});
//對于環繞通知只能添加一個,多添加也是沒有用的,spring會選第一個advice,請看結果
proxyFactory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("1---環繞通知");
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---環繞通知生成的結果:"+new String(result));
return "北京生活圈".getBytes();
}
});
//對于環繞通知只能添加一個,多添加也是沒有用的,spring會選第一個advice,請看結果
proxyFactory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("2---環繞通知");
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---環繞通知生成的結果:"+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("北京")));
}
}
關于Spring api如何手動創建代理對象ProxyFactory就分享到這里啦,希望上述內容能夠讓大家有所提升。如果想要學習更多知識,請大家多多留意小編的更新。謝謝大家關注一下億速云網站!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。