Springapi如何手动创建代理对象ProxyFactory
这篇文章跟大家分析一下“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就分享到这里啦,希望上述内容能够让大家有所提升。如果想要学习更多知识,请大家多多留意小编的更新。谢谢大家关注一下创新互联网站!
网站题目:Springapi如何手动创建代理对象ProxyFactory
转载来源:http://azwzsj.com/article/ijghes.html