zoukankan      html  css  js  c++  java
  • 一个使用Spring的AspectJ LTW的简单例子

    参考:Spring Framework Reference Documentation

       Spring AOP 实现原理与 CGLIB 应用  

       比较分析 Spring AOP 和 AspectJ 之间的差别

    AspectJ是实现AOP编程的一种具体实现

    AspectJ中有两种实现的方式:

    1.使用Ajc编译器在编译器生成代理类

    2.使用AspectJ LTW 在类加载时生成代理

    主要的Bean

    public class DemoBean {
            public void run() {
                System.out.println("Run");
            }
            public void run1() {
                System.out.println("run1...");
            }
            public void run2() throws Exception {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("run2...");
            }
        }

    Aspect

    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.util.StopWatch;
    @Aspect
    public class ProfilingAspect {
    
        @Around("profileMethod()")
        public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            StopWatch sw = new StopWatch(getClass().getSimpleName());
            try {
                sw.start(pjp.getSignature().getName());
                return pjp.proceed();
            } finally {
                sw.stop();
                System.out.println(sw.prettyPrint());
            }
        }
        @Pointcut("execution(* com.jxufe.study.spring.aspect..*.*(..))")
        public void profileMethod() {
    
        }
    
    }

    META-INF/aop.xml   (这个路径和名字是确定的)

    <!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
        <aspectj>
    
            <weaver>
                <!-- only weave classes in our application-specific packages -->
                <include within="com.jxufe.study.spring.aspect.*"/>
            </weaver>
            <aspects>
                <!-- weave in just this aspect -->
                <aspect name="com.jxufe.study.spring.aspect.ProfilingAspect"/>
            </aspects>
    
        </aspectj>

    最后的aspect.xml

    <?xml version="1.0" encoding="GBK"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
        <context:load-time-weaver/>
       <bean id="demoBean" class="com.jxufe.study.spring.aspect.DemoBean"></bean>
    </beans>

    Test类

     public static void main(String[] args) throws Exception {
    
    
            ApplicationContext ctx = new ClassPathXmlApplicationContext("/META-INF/aspect.xml", Main.class);
    /*
            DemoBean demoBean = (DemoBean) ctx.getBean("de");
    */
            DemoBean demoBean  = new DemoBean();
            demoBean.run();
            demoBean.run1();
            demoBean.run2();

    运行时添加 jvm 参数  -javaagent:C:Users1.m2 epositoryorgspringframeworkspring-instrument4.3.9.RELEASEspring-instrument-4.3.9.RELEASE.jar

    输出结果:

    Run
    StopWatch 'ProfilingAspect': running time (millis) = 0
    -----------------------------------------
    ms     %     Task name
    -----------------------------------------
    00000  �  run
    
    run1...
    StopWatch 'ProfilingAspect': running time (millis) = 1
    -----------------------------------------
    ms     %     Task name
    -----------------------------------------
    00001  100%  run1
    
    Disconnected from the target VM, address: '127.0.0.1:52489', transport: 'socket'
    run2...
    StopWatch 'ProfilingAspect': running time (millis) = 2003
    -----------------------------------------
    ms     %     Task name
    -----------------------------------------
    02003  100%  run2

    这里的Test类中,这个Bean中使用的是DemoBean demoBean = new DemoBean();但结果却是实现了AOP的效果。

  • 相关阅读:
    15 手写数字识别-小数据集
    14 深度学习-卷积
    13-垃圾邮件分类2
    12.朴素贝叶斯-垃圾邮件分类
    11.分类与监督学习,朴素贝叶斯分类算法
    9、主成分分析
    7.逻辑回归实践
    8.特征选择,过滤式
    6.逻辑回归
    5.线性回归算法
  • 原文地址:https://www.cnblogs.com/alway-july/p/7793326.html
Copyright © 2011-2022 走看看