前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring api之手动创建代理对象ProxyFactory

Spring api之手动创建代理对象ProxyFactory

作者头像
BUG弄潮儿
发布2022-06-30 14:55:08
3910
发布2022-06-30 14:55:08
举报
文章被收录于专栏:JAVA乐园

可以通过注解的方式来自定义代理对象的创建,同时也可以通过 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("北京")));

}

}

结果:

自己动手丰衣足食~~~~

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-02-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档