SpringAPI手动建立代理对象——ProxyFactory

能够经过注解的方式来自定义代理对象的建立,同时也能够经过SpringAPI,手动编程的方式来建立代理对象。java

几个重要的API:spring

ProxyFactory\MethodInterceptor\Advice\AfterReturningAdvice\MethodBeforeAdvice编程

直接粘贴代码,代码能说明一切大笑ide


/**
*
*/
package cn.hessian.proxy;ui

import java.lang.reflect.Method;this

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;.net

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 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在方法调用以前拦截");
            }
        });
        //能够添加多个方法前置或者后置通知
    proxyFactory.addAdvice(new MethodBeforeAdvice() {
           
            @Override
            public void before(Method method, Object[] args, Object target)
                    throws Throwable {
                System.out.println("22222222在方法调用以前拦截");
            }
        });
   //能够添加多个方法前置或者后置通知
        proxyFactory.addAdvice(new AfterReturningAdvice() {
           
            @Override
            public void afterReturning(Object returnValue, Method method,
                    Object[] args, Object target) throws Throwable {
                System.out.println("方法完成以后调用的方法11111");
               
            }
        });
       
       //能够添加多个方法前置或者后置通知
        proxyFactory.addAdvice(new AfterReturningAdvice() {
           
            @Override
            public void afterReturning(Object returnValue, Method method,
                    Object[] args, Object target) throws Throwable {
                System.out.println("方法完成以后调用的方法22222");
               
            }
        });
      接口

  //对于环绕通知只能添加一个,多添加也是没有用的,spring会选第一个advice,请看结果

        proxyFactory.addAdvice(new MethodInterceptor() {
           
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("1111111环绕通知");
                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环绕通知生成的结果--"+new String(result));
                return "北京生活圈".getBytes();
            }
        });
       
       //对于环绕通知只能添加一个,多添加也是没有用的,spring会选第一个advice,请看结果
proxyFactory.addAdvice(new MethodInterceptor() {
           
            @Override
            public Object invoke(MethodInvocation invocation) throws Throwable {
                System.out.println("22222环绕通知");
                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环绕通知生成的结果--"+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("北京")));
    }
}

 



生成的结果为大笑:

HelloWorldService
SpringProxy
1111111111在方法调用以前拦截
22222222在方法调用以前拦截
1111111环绕通知
1111111111环绕通知生成的结果--你好 北京
方法完成以后调用的方法22222
方法完成以后调用的方法11111
北京生活圈

Penguins

相关文章
相关标签/搜索