zoukankan      html  css  js  c++  java
  • 针对spring--AOP的理解及例子

    1.了解AOP的相关术语

    1.通知(Advice):

    通知定义了切面是什么,以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。

    2.连接点(Joinpoint):

    程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。

    3.切入点(Pointcut):

    通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,Spring中允许我们方便的用正则表达式来指定(切面在哪个方法的前或后做出的那个方法点)

    4.切面(Aspect):

    通知和切入点共同组成了切面:时间、地点和要发生的“故事”,事务管理是J2EE应用中一个很好的横切关注点例子,切面用Spring的Advisor或拦截器实现

    5.引入(Introduction):

    引入允许我们向现有的类添加新的方法和属性(Spring提供了一个方法注入的功能)

    6.目标(Target):

    即被通知的对象,如果没有AOP,那么它的逻辑将要交叉别的事务逻辑,有了AOP之后它可以只关注自己要做的事(AOP让他做爱做的事)

    7.代理(proxy):

    应用通知的对象,详细内容参见设计模式里面的代理模式

    8.织入(Weaving):

    把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:

    (1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器

    (2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码

    (3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术

    存在的实现方式

    1.经典的基于代理的AOP 
    2.@AspectJ注解驱动的切面 
    3.纯POJO切面 
    4.注入式AspectJ切面

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

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

    接口:

    复制代码
    package com.itnba.test;
    
    public interface IHuman {
        public void chifan();
        public void shuijiao();
    
    }
    复制代码

    实现接口的两个类:

    复制代码
    package com.itnba.test;
    
    import org.springframework.stereotype.Component;
    
    public class Chinese implements IHuman {
    
        @Override
        public void chifan() {
            // TODO 自动生成的方法存根
            System.out.println("中国人吃饭");
    
        }
        
        @Override
        public void shuijiao() {
            // TODO 自动生成的方法存根
            System.out.println("中国人睡觉");
        }
    
    }
    复制代码
    复制代码
    package com.itnba.test;
    
    import org.springframework.stereotype.Component;
    
    
    public class American implements IHuman {
    
        @Override
        public void chifan() {
            // TODO 自动生成的方法存根
            System.out.println("美国人吃饭");
    
        }
    
        @Override
        public void shuijiao() {
            // TODO 自动生成的方法存根
            System.out.println("美国人睡觉");
        }
    
    
    }
    复制代码

    3.定义一个切面类

    复制代码
    package com.itnba.test;
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    public class Qiemian {
        
        public void chifanqian(){
            System.out.println("洗手");
        }
        public void chifanhou(){
            System.out.println("漱口");
        }
        public void shuijiaoqian(){
            System.out.println("洗澡");
            
        }
    
    }
    复制代码

    4.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/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"
            default-autowire="byName"
            >
             <!-- 首相要实例化目标对象类和切面类 -->
            <bean id="chinese" class="com.itnba.test.Chinese"></bean>
            <bean id="american" class="com.itnba.test.American"></bean>
            <bean id="qiemian" class="com.itnba.test.Qiemian"></bean>
            
            <aop:config>
            <!-- 要切入的对象 -->                 
            <aop:pointcut expression="execution(* com.itnba.test.*.chifan(..))" id="chifan"/>
            <aop:pointcut expression="execution(* com.itnba.test.*.shijiao(..))" id="shijiao"/>
            <!-- 切入点 -->
            <aop:aspect id="ha" ref="qiemian"><!-- 切面类  -->
                <!--  <aop:之前before、之后after... method="切面类中的方法" pointcut-ref="上面的切入的对象"/>  -->
                <aop:before method="chifanqian()" pointcut-ref="chifan"/><!-- 之前通知 -->
                <aop:after method="chifanhou()()" pointcut-ref="chifan"/><!-- 之后通知 -->
                <aop:before method="shuijiaoqian()" pointcut-ref="shijiao"/>
            </aop:aspect>
        </aop:config>
    </beans>
    复制代码

    5.运行

    复制代码
    package com.itnba.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            //要用接口接收
            IHuman c= (IHuman)context.getBean("chinese");
            c.chifan();
            c.shuijiao();
            System.out.println("*********************************");
            //要用接口接收
            IHuman a= (IHuman) context.getBean("american");
            a.chifan();
            a.shuijiao();
    
        }
    
    }
  • 相关阅读:
    用WinForm写的员工考勤项目!!!!!!
    洛谷P1892《[BOI2003]团伙》
    洛谷P1821《[USACO07FEB]银牛派对Silver Cow Party》
    洛谷P1149《火柴棒等式》
    2017 国庆清北刷题冲刺班《角谷猜想》
    洛谷P2330《[SCOI2005]繁忙的都市》
    洛谷P1955《[NOI2015]程序自动分析》
    洛谷P1536《村村通》
    Windows 10 体验记
    洛谷P1102《A-B数对》
  • 原文地址:https://www.cnblogs.com/zhaoyfk/p/8628909.html
Copyright © 2011-2022 走看看