zoukankan      html  css  js  c++  java
  • Spring的AOP配置文件和注解实例解析

    1.1           Spring的AOP配置文件和注解实例解析

    AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。例如打印日志。与核心业务逻辑无关,但是却贯穿整个程序当中。所以使用AOP切面技术将日志和核心业务逻辑分离开来,通过配置文件或者注解配置切面,并为切面设置过滤条件,过滤条件是程序中的方法,表示调用程序中的这些方法时会调用日志切面的方法来打印日志,相当于是一种过滤符合条件就触发打印日志的机制。而不是将日志和程序中的方法写在一起。

    1.1.1            AOP配置文件方式

    通过配置文件设置切面,切入点,设置与切入点关联的触发方法,配置核心业务函数与触发函数之间的映射关系。一旦触发方法调用,就会进入切面,在触发方法前、后、异常等情况下执行相关函数。

    (1)为日志类LogAop配置切面信息,配置applicationContext.xml 中logAopBean是配置日志的bean,提供给IOC调用。aop:aspect id="logAspect"则是定义一个切面。

    <!-- 配置日志类logAopBean为bean ,提供给IOC使用-->

    <bean id="logAopBean" class="com.demo.common.aop.LogAop"></bean>

    <aop:config>

    <!—设置切面,logAopBean是日志类的名称 -->

            <aop:aspect id="logAspect" ref="logAopBean">

    <!—设置切入点, expression指定过滤条件,表示com.demo 内部的所有方法,id指定切入的方法,这个方法是一个空函数,只是用于后面关联日志类中要调用的其他方法-->

                <aop:pointcut expression="execution(* com.demo..*(..))" id="allMethod"/>

    <!—com.demo中的方法,在调用时,就会触发日志类中的函数,下面四个方法分别是调用前打日志,调用异常打日志,调用返回打日志,调用结束打日志,切入点allMethod和核心业务类中的方法关联,日志类中的方法和切入点方法关联。核心方法调用时,触发切入点,切入点根据方法的执行情况去执行对应的日志方法。 -->

                <aop:before method="before" pointcut-ref="allMethod" />

                <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod" />

                <aop:after-returning method="afterReturn" pointcut-ref="allMethod" />

                <aop:after method="after" pointcut-ref="allMethod" />

            </aop:aspect>

        </aop:config>

    (2)定义日志类,实现日志类中前置、后置、异常、返回等方法。

    package com.demo.common.aop;

    import org.aspectj.lang.JoinPoint;

    import org.aspectj.lang.ProceedingJoinPoint;

    public class LogAop {

        public void before(JoinPoint call){

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println("开始执行:"+className+"."+methodName+"()方法...");

        }

        public void afterThrowing(JoinPoint call){

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println(className+"."+methodName+"()方法抛出了异常...");

        }

        public void afterReturn(JoinPoint call){

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println(className+"."+methodName+"()方法正常执行结束...");

        }

        public void after(JoinPoint call){

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println(className+"."+methodName+"()最终执行步骤(finally)...");

        }

        /*//用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型 

        public Object doAround(ProceedingJoinPoint call) throws Throwable { 

            Object result = null; 

            this.before(call);//相当于前置通知 

            try { 

                result = call.proceed(); 

                this.afterReturn(call); //相当于后置通知 

            } catch (Throwable e) { 

                this.afterThrowing(call); //相当于异常抛出后通知 

                throw e;

            }finally{ 

                this.after(call);  //相当于最终通知 

            } 

            return result; 

        }*/

    }

    1.1.2            AOP注解方式

    使用注解去代替配置文件,告诉IOC容器,切面、切入点、触发函数和核心业务方法之间的映射关系。前置方法、后置方法、异常方法、正常返回方法。

    在配置文件中声明LogAnnotationAspect为logAspectBean,告诉IOC容器这是一个bean。

    <bean id="logAspectBean" class="com.demo.common.aop.LogAnnotationAspect"></bean>

        <aop:aspectj-autoproxy/>

    package com.demo.common.aop;

    import org.aspectj.lang.JoinPoint;

    import org.aspectj.lang.ProceedingJoinPoint;

    import org.aspectj.lang.annotation.After;

    import org.aspectj.lang.annotation.AfterReturning;

    import org.aspectj.lang.annotation.AfterThrowing;

    import org.aspectj.lang.annotation.Aspect;

    import org.aspectj.lang.annotation.Before;

    import org.aspectj.lang.annotation.Pointcut;

    @Aspect  //定义切面类 

    public class LogAnnotationAspect { 

        @SuppressWarnings("unused") 

        //定义切入点,提供一个方法,这个方法的名字就是切入点的id 

        @Pointcut("execution(* com.demo..*(..))")  //关联核心业务函数

        private void allMethod(){} 

        //针对指定的切入点表达式选择的切入点应用前置通知 

        @Before("allMethod()")   

        public void before(JoinPoint call) { 

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println("开始执行:"+className+"."+methodName+"()方法...");

        } 

        //访问命名切入点来应用后置通知 

        @AfterReturning("allMethod()") 

        public void afterReturn(JoinPoint call) { 

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println(className+"."+methodName+"()方法正常执行结束...");

        } 

        //应用最终通知 

        @After("allMethod()") 

        public void after(JoinPoint call) { 

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println(className+"."+methodName+"()最终执行步骤(finally)...");

        } 

        //应用异常抛出后通知 

        @AfterThrowing("allMethod()") 

        public void afterThrowing(JoinPoint call) { 

            String className = call.getTarget().getClass().getName();

            String methodName = call.getSignature().getName();

            System.out.println(className+"."+methodName+"()方法抛出了异常...");

        } 

        //应用周围通知 

        //@Around("allMethod()") 

        public Object doAround(ProceedingJoinPoint call) throws Throwable{ 

            Object result = null; 

            this.before(call);//相当于前置通知 

            try { 

                result = call.proceed(); 

                this.afterReturn(call); //相当于后置通知 

            } catch (Throwable e) { 

                this.afterThrowing(call);  //相当于异常抛出后通知 

                throw e; 

            }finally{ 

                this.after(call);  //相当于最终通知 

            } 

            return result; 

        } 

    }

    自己编了一个股票监控软件,有如下功能,有兴趣的朋友可以下载;

    (1)   个股监测。监测个股实时变化,可以监测个股大单交易、急速拉升和下降、主力入场和出场、股票最高点和最低点提醒。检测到最高点、最低点、主力进场点、主力退场点、急速拉升点、急速下跌点,给出语音或者声音提醒,不用再时刻看着大盘了,给你更多自由的时间;

    (2)   大盘监测。监测大盘的走势,采用上证、深证、创业三大指数的综合指数作为大盘走势。并实时监测大盘的最高点和最低点、中间的转折点。

    (3)   股票推荐。还能根据历史数据长期或短期走势进行分析,对股市3千多个股票进行分析对比,选出涨势良好的股票,按照增长速度从大到小排序,推荐给你涨势良好的股票;

    下载地址:

    1.0.3版本(修复大盘指数崩溃缺陷)下载地址:

    链接:https://pan.baidu.com/s/1BJcTp-kdniM7VE9K5Kd3vg 提取码:003h

    更新链接:

    https://www.cnblogs.com/bclshuai/p/10621613.html

  • 相关阅读:
    SpringMVC 集成 Swagger【问题】 No qualifying bean of type RequestMappingHandlerMapping found for dependency
    【leetcode】medianofTwoSortedArrays
    记一次apache+php调优
    java 文件定位
    Java知识探究一:关于IO类库
    180app待选内容
    SQlserver 2000 根据spid 查询执行的SQL
    (转)Flashbuilder4.5中文版破解方法
    代码整洁之道
    手机分辨率基础知识(DPI,DIP计算)
  • 原文地址:https://www.cnblogs.com/bclshuai/p/10298601.html
Copyright © 2011-2022 走看看