zoukankan      html  css  js  c++  java
  • Spring AOP(面向切面示例)

    什么是AOP?
    基本概念
    切面(aspect):横切关注点被模块化的特殊对象。
    通知(advice):切面必须要完成的工作。切面中的每个方向称之为通知。通知是在切面对象中的。
    目标(target):被通知的对象。
    代理(proxy):向目标对象应用通知后创建的对象。

    连接点(joinpoint):目标对象的程序执行的某个特定位置。如某个方法调用前,调用后的位置。包括两个信息:1.目标程序的哪个方法?2.方法执行
    前还是执行后?
    切点(pointcut):每个类会有多个连接点,AOP通过切点定位到特定的边接点。
    类比,连接点相当于数所成方圆 中的记录,而切点相当于查询条件。一个切点匹配多个连接点。

    1.用XML的方式来实现Spring的面向切面

    1.首先定义一个切面类

    package com.maya.aop2;
    public class AOP {
        public void beforEat(){
            System.out.println("吃饭前洗手");
        }
        public void afterEat(){
            System.out.println("吃饭后刷碗");
        }
        
        public void beforSeelp(){
            System.out.println("睡觉前洗脸");
        }    
        public void afterSeelp(){
            System.out.println("睡觉后吃饭");
        }
    }

    2.目标对象(要切入的对象)

    首先为了不违反开闭原则和更好的可扩展性,目标对象实际上是实现了已定义好的某个接口;

    package com.maya.model2.copy;
    public interface IHumn {
        public void eat();
        public void sleep();
    }

    目标对象Chinese

    package com.maya.model2.copy;
    
    public class Chinese implements IHumn {
        @Override
        public void eat() {
            // TODO 自动生成的方法存根
            System.out.println("中国人在吃饭");
        }
        @Override
        public void sleep() {
            // TODO 自动生成的方法存根
            System.out.println("中国人在碎觉");
        }
    
    }

    XML

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
        <!-- 首相要实例化目标对象类和切面类 -->
        <bean id="aop" class="com.maya.aop2.AOP"></bean>
        <bean id="chinese" class="com.maya.model2.copy.Chinese"></bean>
        <bean id="usa" class="com.maya.model2.copy.USA"></bean>
            
        <aop:config>
            <!-- 要切入的对象 -->                 
            <aop:pointcut expression="execution(* com.maya.model2.copy.*.eat())" id="eat"/>
            <aop:pointcut expression="execution(* com.maya.model2.copy.*.sleep())" id="sleep"/>
            <!-- 切入点 -->
            <aop:aspect id="adtAop" ref="aop">
                <aop:before method="beforEat()" pointcut-ref="eat"/><!-- 之前通知 -->
                <aop:after method="afterEat()" pointcut-ref="eat"/><!-- 之后通知 -->
                <aop:before method="beforSleep()" pointcut-ref="sleep"/>
                <aop:after method="afterSleep()" pointcut-ref="sleep"/>
            </aop:aspect>
        </aop:config>
        
    </beans>

    二、注解(Annotation)方式

    1.首先也是要定义一个切面类

    package com.maya.aop;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    @Aspect//声明该类是切面类
    @Component//配置文件中启动自动扫描功能
    public class AOP {
        @Before("execution(* com.maya.model2.*.eat(..))")//之前通知
        public void beforEat(){
            System.out.println("吃饭前洗手");
        }
        @After("execution(* com.maya.model2.*.eat(..))")//之后通知
        public void afterEat(){
            System.out.println("吃饭后刷碗");
        }
        
        @Before("execution(* com.maya.model2.*.seelp(..))")
        public void beforSleep(){
            System.out.println("睡觉前洗脸");
        }
        @After("execution(* com.maya.model2.*.seelp(..))")
        public void afterSleep(){
            System.out.println("睡觉后吃饭");
        }
    }

    2.首先也是要定义一个接口(同上)

    目标对象Chinese

    package com.maya.model2;
    import org.springframework.stereotype.Component;
    @Component//配置文件中启动自动扫描功能
    public class Chinese implements IHumn {
        @Override
        public void eat() {
            // TODO 自动生成的方法存根
            System.out.println("中国人在吃饭");
        }
        @Override
        public void sleep() {
            // TODO 自动生成的方法存根
            System.out.println("中国人在碎觉");
        }
    }

    3.XML

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        default-autowire="byName"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        <!-- 自动扫描包下的类,并将其实例化。多个包之间用,隔开 -->
        <context:component-scan base-package="com.maya.model2,com.maya.aop"></context:component-scan>
        <!-- 配置文件中启动AspectJ的注解功能 ,默认是false,要将其改为true -->
        <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>          
    </beans>

    这样,Spring的面向切面编程就简单介绍完了。

    通知分类:
    前置通知(@Before) -- 在目标方法执行前执行的通知。
    后置通知(@After) -- 在目标方法执行后执行的通知。无论是否发生异常。后置通知中,无法读取目标方法返回的结果。
    返回通知(@AfterReturnning) --在目标方法执行成功后执行的通知。在返回通知中可以访问目标返回结果的。
    @AfterReturnning(value="execution(* com.itnba..*(..))",returning="result")
    public void afterRun(Object result){
    System.out.println(result);
    }

    异常通知(@AfterThrowing) -- 在目标方法执行出错后执行的通知。
    @AfterThrowing(value="execution(* com.itnba..*(..))",throwing="ex")
    public void afterThrowing(Exception ex){
    System.out.println(ex.getMessage());
    }

    环绕通知(@Around) -- 需要切面方法携带ProceedingJoinPoion参数,类似于一个完整的动态代理,环绕通知必须要有一个返回值,是目标方法的返
    回值。

    @Around("execution(* com.itnba..*(..))")
    public object aroundAdvice( ProceedingJoinPoint pjp){

    object obj = null;
    try{
    //做前置通知
    obj = pjp.proceed();
    //做返回通知
    }
    catch(Exception ex){
    //做异常通知
    }
    //做后置通知
    return obj;
    }

    添加日志:

    切面方法可以加入参数(JoinPoint) joinPost.getSignature().getXXX()获得相关方法信息
    切面方法中可以加入Object参数,用来获得目标方法的返回值(只对返回通知起作用)

  • 相关阅读:
    JVM调优
    【转】中文分词之HMM模型详解
    xwiki enterprise 8.4.5使用https步骤
    dubbo和shiro的整合,在服务端做权限验证
    电商课题:分布式锁
    Export large data from Gridview and Datareader to an Excel file using C#
    sap学习笔记
    git拉取远程分支并创建本地分支
    Echarts 解决饼图文字过长重叠的问题
    用localStorage坚持Vuex状态----vuex-persistedstate
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6635427.html
Copyright © 2011-2022 走看看