`

sprig AOP之ProxyFactory

阅读更多
我们在项目中大多使用的声明式的AOP配置,其实在spring中也提供了编程式的AOP实现方法,具体在ProxyFactory中例如ProxyFactory例子如下:

TargetImpl target = new TargetImpl();

ProxyFactory proxy = new ProxyFactory ();

target.addAdvisor(youAdvisor);

target.addAdvice(youAdvice);

TargetImpl  targetproxy = proxy.getProxy();

proxyFactory类的源码如下:
public class ProxyFactory extends ProxyCreatorSupport
{

    public ProxyFactory()
    {
    }

    public ProxyFactory(Object target)
    {
        Assert.notNull(target, "Target object must not be null");
        setInterfaces(ClassUtils.getAllInterfaces(target));
        setTarget(target);
    }

    public ProxyFactory(Class proxyInterfaces[])
    {
        setInterfaces(proxyInterfaces);
    }

    public ProxyFactory(Class proxyInterface, Interceptor interceptor)
    {
        addInterface(proxyInterface);
        addAdvice(interceptor);
    }

    public ProxyFactory(Class proxyInterface, TargetSource targetSource)
    {
        addInterface(proxyInterface);
        setTargetSource(targetSource);
    }

    public Object getProxy()
    {
        return createAopProxy().getProxy();
    }

    public Object getProxy(ClassLoader classLoader)
    {
        return createAopProxy().getProxy(classLoader);
    }

    public static Object getProxy(Class proxyInterface, Interceptor interceptor)
    {
        return (new ProxyFactory(proxyInterface, interceptor)).getProxy();
    }

    public static Object getProxy(Class proxyInterface, TargetSource targetSource)
    {
        return (new ProxyFactory(proxyInterface, targetSource)).getProxy();
    }

    public static Object getProxy(TargetSource targetSource)
    {
        if(targetSource.getTargetClass() == null)
        {
            throw new IllegalArgumentException("Cannot create class proxy for TargetSource with null target class");
        } else
        {
            ProxyFactory proxyFactory = new ProxyFactory();
            proxyFactory.setTargetSource(targetSource);
            proxyFactory.setProxyTargetClass(true);
            return proxyFactory.getProxy();
        }
    }
}
proxyFactory的实现原理与ProxyFactoryBean的实现原理是一样的,只是在最外层的表示形式有所不同,proxyFactory没有使用proxyFactoryBean的IoC封装,而是通过直接继承ProxyCreatorSupport的功能来完成AOP的属性配置。

具体实例
/**
* 具体的拦截器类
*/
public class TestInterceptor implements MethodInterceptor
{
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable
    {
        System.out.print("zhangsan start.....");
        Object retVal = invocation.proceed();
        System.out.print("end");
        return retVal;
    }

}

//具体业务类
public class Loginservice
{
   public void login()
   {
       System.out.println("this is login method");
   }
}

//测试类
public class Test
{
    public static void main(String[] args)
    {
        //具体的业务逻辑
        Loginservice loginservice = new Loginservice();
        //代理工厂
        ProxyFactory pf = new ProxyFactory(loginservice);
        //添加advice
        pf.addAdvice(new TestInterceptor());
        //产生代理对象
        Loginservice lg = (Loginservice) pf.getProxy();
        lg.login();
    }
}

这只是简单的通过编码完成了spring的AOP功能。





分享到:
评论
2 楼 liuwuhen 2015-08-13  
代码还是不错的,头像确实有点
1 楼 西蜀石兰 2015-07-13  
这头像。。。。完全不用看代码了

相关推荐

    aop的详细总结(包含例子)

    一、AOP 概念 二、AOP 种类 三、Spring AOP 代理原理 四、Spring AOP 通知类型 五、Spring AOP Pointcut 六、Spring AOP 中的Advisor其实就是Aspect 七、AOP ProxyFactory 八、自动代理

    Spring AOP 自动代理源码 DefaultAdvisorAutoProxyCreator

    Spring AOP源码02:ProxyFactory Spring AOP源码03:JdkDynamicAopProxy Spring AOP源码04:MethodInvocation 拦截器调用 Spring AOP源码05:DefaultAdvisorAutoProxyCreator Spring期末考压轴题:当Spring AOP遇上...

    spring-aop-ProxyFactoryBean 源码分析

    NULL 博文链接:https://wangxinchun.iteye.com/blog/2079585

    Spring-Reference_zh_CN(Spring中文参考手册)

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.1.1. BeanNameAutoProxyCreator 7.9.1.2. DefaultAdvisorAutoProxyCreator ...

    Spring 2.0 开发参考手册

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.2. 使用元数据驱动的自动代理 7.10. 使用TargetSources 7.10.1. 热交换...

    spring.net中文手册在线版

    12.6.使用ProxyFactory类以编程方式创建AOP代理 12.7.管理目标对象 12.8.使用“自动代理”功能 12.8.1.自动代理对象的定义 12.8.1.1.ObjectNameAutoProxyCreator 12.8.1.2.DefaultAdvisorAutoProxyCreator 12.8.1.3....

    AOP 和 Aspect 注解切入 测试 Demo

    1.ProxyFactory 基于 MethodBeforeAdvice、AfterReturningAdvice 利用 Spring Api 定义前、后置处理方法,并通过代理工厂类获取代理对象(代码或Xml配置实现) 2.ProxyFactoryBean 显式地设置 Advisors、Advice、...

    Spring中文帮助文档

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.2. 使用元数据驱动的自动代理 7.10. 使用TargetSource 7.10.1. 热交换...

    spring chm文档

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.2. 使用元数据驱动的自动代理 7.10. 使用TargetSources 7.10.1. 热交换...

    Spring API

    7.7. 使用ProxyFactory通过编程创建AOP代理 7.8. 操作被通知对象 7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.2. 使用元数据驱动的自动代理 7.10. 使用TargetSource 7.10.1. 热交换...

    AOP实现计算器代码.zip

    使用ProxyFactory或ProxyFactoryBean实现计算器功能,同时(1)日志功能:在程序执行期间追踪正在发生的活动(打印出调用的方法,以及参数的参数值);(2)验证功能:希望计算器只能处理正数的运算,当有负数参与...

    first Spring test

    1.实现特定功能的方面代码在aop中又称为“通知(Advice)”包含 前置通知 后置通知 环绕...2.spring 采用代理的方式将通知织入到原bean中,将原bean和通知都封装到--org.springframework.aop.framework.ProxyFactory.

    Java Web程序设计教程

    12.4springaop的代理工厂 253 12.4.1选择合适的代理 253 12.4.2proxyfactory 254 12.4.3proxyfactorybean 254 12.5项目实战——输出日志 256 本章小结 258 课后练习 259 第13章 spring与javaee持久化数据...

    SPRING API 2.0.CHM

    All Classes ...AopContext AopInvocationException AopNamespaceHandler AopNamespaceUtils AopProxy AopProxyFactory AopProxyUtils AopUtils ApplicationContext ApplicationContextAware ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    tyloo:分布式交易框架——TCC

    基于Spring AOP切面思想实现对分布式事务注解的拦截。 基于Dubbo的ProxyFactory代理机制为服务接口生成代理对象。 基于Mysql,Redis乐观锁进行事务版本控制以及基于石英进行事务恢复。 支持各种事务日志序列化以及...

    mobileSenderDemo

    Spring Data Jpa应用程序API API1.1。 기본API가인터페이스상속하여사용 반스않고스스스스스스함함함... AOP倒影기술을활용함 ProxyFactory,反射,MethodInterceptor,JdkDynamicAopProxy,CGLib Spring数据Jpa소스를

Global site tag (gtag.js) - Google Analytics