`
NoWhy
  • 浏览: 34326 次
  • 性别: Icon_minigender_1
  • 来自: 他乡
社区版块
存档分类
最新评论

S2Aop浅析

    博客分类:
  • Java
阅读更多
java类

public class MyCls {
    public void add(int a, int b){
        System.out.println(a+b);
    }
}

拦截器
import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.aop.interceptors.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 8348047103038121708L;

    public Object invoke(MethodInvocation invocation) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("BEGIN");
        Object ret = invocation.proceed();
        System.out.println("END");
        return ret;
    }

}

配置
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">
<components>
    <include path="dao.dicon"/>
    <component class="jp.co.test.MyCls">
        <aspect pointcut="add">
            <component class="jp.co.test.interceptor.MyInterceptor"/>
        </aspect>
    </component>
</components>

调用
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        S2Container container = S2ContainerFactory.create("testapp.dicon");
        container.init();
        MyCls myCls = (MyCls) container.getComponent(MyCls.class);
        myCls.add(1, 2);
    }


java源文件
org/seasar/framework/aop/javassist/AbstractGenerator.java

的173行
public Class toClass(final ClassLoader classLoader, final CtClass ctClass) 

中体添加以下代码
            File file = new File("D:\\gencls\\cls\\"+ctClass.getSimpleName()+".class");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytecode);
            fos.flush();
            fos.close();

把动态生成的类保存起来。

把类反编译一下
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MyCls$$EnhancedByS2AOP$$9f671b.java

package jp.co.test;

import java.lang.reflect.UndeclaredThrowableException;

// Referenced classes of package jp.co.test:
//            MyCls

public class MyCls$$EnhancedByS2AOP$$9f671b extends MyCls
{

    public void add$$invokeSuperMethod$$(int i, int j)
    {
        super.add(i, j);
        Object _tmp = null;
    }

    public void add(int i, int j)
    {
        try
        {
            class .MethodInvocation..add0
                implements S2MethodInvocation
            {

                public Class getTargetClass()
                {
                    return targetClass;
                }

                public Method getMethod()
                {
                    return method;
                }

                public AccessibleObject getStaticPart()
                {
                    return method;
                }

                public Object getParameter(String name)
                {
                    if(parameters == null)
                        return null;
                    else
                        return parameters.get(name);
                }

                public Object getThis()
                {
                    return target;
                }

                public Object[] getArguments()
                {
                    return arguments;
                }

                public Object proceed()
                    throws Throwable
                {
                    if(interceptorsIndex < interceptors.length)
                    {
                        return interceptors[interceptorsIndex++].invoke(this);
                    } else
                    {
                        ((MyCls..EnhancedByS2AOP.._cls9f671b)target).add$$invokeSuperMethod$$(((Number)arguments[0]).intValue(), ((Number)arguments[1]).intValue());
                        return null;
                    }
                }

                private static Class targetClass;
                private static Method method;
                private static MethodInterceptor interceptors[];
                private static Map parameters;
                private Object target;
                private Object arguments[];
                private int interceptorsIndex;

            public .MethodInvocation..add0(Object target, Object arguments[])
            {
                this.target = target;
                this.arguments = arguments;
            }
            }

            Object obj = (new .MethodInvocation..add0(this, new Object[] {
                new Integer(i), new Integer(j)
            })).proceed();
            return;
        }
        catch(RuntimeException runtimeexception)
        {
            throw runtimeexception;
        }
        catch(Error error)
        {
            throw error;
        }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public MyCls$$EnhancedByS2AOP$$9f671b()
    {
    }
}

分享到:
评论

相关推荐

    spring aop spring aop

    spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 aopalliance-1.0

    开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...

    基于注解实现SpringAop

    基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop

    aop例子aop例子

    自己写的aop代理spring

    AOP使用CGLIB实现AOP功能

    Spring AOP实现方法之一:CGLIB 实现AOP功能

    aop所依赖的所有包

    aop所依赖的所有包+文档+源码,最新版全套aop aspectjweaver aopalliance aspects aspectjrt

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring aop jar 包

    spring aop jar 包

    反射实现 AOP 动态代理模式(Spring AOP 的实现原理)

    AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理

    android aop

    自己写的android aop的例子 可以说明在android开发中如何使用aop编码的

    spring-boot aop

    源代码是spring-boot添加aop切面功能的demo,简单易懂.

    spring中AOP中标签加载通知

    spring中AOP中标签加载通知,spring中AOP中标签加载通知spring中AOP中标签加载通知spring中AOP中标签加载通知,spring中AOP中标签加载通知

    AOP@Work AOP 工具比较

    概述 AOP 工具当前的技术状况,比较对于该技术而言最成熟的一些方法:AspectJ、AspectWerkz、JBoss AOP、和 Spring AOP,并对比与每种方法的采用有关的问题。

    spring-aop实例

    spring aop

    AOP联盟:aopalliance.jar API包

    AOP联盟:aopalliance.jar API包a set of AOP Java interface .

    Spring_aop源码

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象...

    aop的demo和资料

    SPRING AOP

    Spring aop 之 静态代理 动态代理 Aspectj aop-config 等实现方式

    主要对Spring AOP的相关概念和简单的静态代理、动态代理以及常见的几种AOP配置方式做总结学习。主要包括:1. AOP的常见概念 2. 静态代理 3. jdk动态代理 4. Aspectj and Aspectjweaver 5. **aop-config** 6. CGLIB ...

    Spring AOP 16道面试题及答案.docx

    描述一下Spring AOP? 在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介...

Global site tag (gtag.js) - Google Analytics