javascript
SpringAPI手动创建代理对象——ProxyFactory
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
可以通過(guò)注解的方式來(lái)自定義代理對(duì)象的創(chuàng)建,同時(shí)也可以通過(guò)SpringAPI,手動(dòng)編程的方式來(lái)創(chuàng)建代理對(duì)象。
幾個(gè)重要的API:
ProxyFactory\MethodInterceptor\Advice\AfterReturningAdvice\MethodBeforeAdvice
直接粘貼代碼,代碼能說(shuō)明一切:
/**
*
*/
package cn.hessian.proxy;
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(){
??????? //代理對(duì)象需要的實(shí)現(xiàn)的接口
??????? Class[] interfaces=new Class[]{HelloWorldService.class};
??????? //利用spring的API,創(chuàng)建代理工廠
??????? ProxyFactory proxyFactory=new ProxyFactory(interfaces);
??????? //設(shè)置目標(biāo)對(duì)象
??????? proxyFactory.setTarget(new HelloWorldServiceImpl2());
??????? /**
???????? * 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("1111111111在方法調(diào)用之前攔截");
??????????? }
??????? });
??????? //可以添加多個(gè)方法前置或者后置通知
??? proxyFactory.addAdvice(new MethodBeforeAdvice() {
???????????
??????????? @Override
??????????? public void before(Method method, Object[] args, Object target)
??????????????????? throws Throwable {
??????????????? System.out.println("22222222在方法調(diào)用之前攔截");
??????????? }
??????? });
?? //可以添加多個(gè)方法前置或者后置通知
??????? proxyFactory.addAdvice(new AfterReturningAdvice() {
???????????
??????????? @Override
??????????? public void afterReturning(Object returnValue, Method method,
??????????????????? Object[] args, Object target) throws Throwable {
??????????????? System.out.println("方法完成之后調(diào)用的方法11111");
???????????????
??????????? }
??????? });
???????
?????? //可以添加多個(gè)方法前置或者后置通知
??????? proxyFactory.addAdvice(new AfterReturningAdvice() {
???????????
??????????? @Override
??????????? public void afterReturning(Object returnValue, Method method,
??????????????????? Object[] args, Object target) throws Throwable {
??????????????? System.out.println("方法完成之后調(diào)用的方法22222");
???????????????
??????????? }
??????? });
?????
? //對(duì)于環(huán)繞通知只能添加一個(gè),多添加也是沒(méi)有用的,spring會(huì)選第一個(gè)advice,請(qǐng)看結(jié)果
??????? proxyFactory.addAdvice(new MethodInterceptor() {
???????????
??????????? @Override
??????????? public Object invoke(MethodInvocation invocation) throws Throwable {
??????????????? System.out.println("1111111環(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("1111111111環(huán)繞通知生成的結(jié)果--"+new String(result));
??????????????? return "北京生活圈".getBytes();
??????????? }
??????? });
???????
?????? //對(duì)于環(huán)繞通知只能添加一個(gè),多添加也是沒(méi)有用的,spring會(huì)選第一個(gè)advice,請(qǐng)看結(jié)果
proxyFactory.addAdvice(new MethodInterceptor() {
???????????
??????????? @Override
??????????? public Object invoke(MethodInvocation invocation) throws Throwable {
??????????????? System.out.println("22222環(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("222222環(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("北京")));
??? }
}
?
生成的結(jié)果為:
HelloWorldService
SpringProxy
1111111111在方法調(diào)用之前攔截
22222222在方法調(diào)用之前攔截
1111111環(huán)繞通知
1111111111環(huán)繞通知生成的結(jié)果--你好 北京
方法完成之后調(diào)用的方法22222
方法完成之后調(diào)用的方法11111
北京生活圈
轉(zhuǎn)載于:https://my.oschina.net/hotbain/blog/119420
總結(jié)
以上是生活随笔為你收集整理的SpringAPI手动创建代理对象——ProxyFactory的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到自己严重脱发是什么预兆
- 下一篇: 关于Pac-Man,你所要了解的 一切