zoukankan      html  css  js  c++  java
  • Spring aop入门

    AOP核心概念

    1、横切关注点

    对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

    2、切面(aspect)

    类是对物体特征的抽象,切面就是对横切关注点的抽象

    3、连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

    4、切入点(pointcut)

    对连接点进行拦截的定义

    5、通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

    6、目标对象

    代理的目标对象

    7、织入(weave)

    将切面应用到目标对象并导致代理对象创建的过程

    8、引入(introduction)

    在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

    进入实例代码 

    1、

    package com.longteng.lesson2.dao;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public interface HelloWorld {
        void printHelloWorld();
        void doPrint();
    }

    2、

    package com.longteng.lesson2.dao.impl;
    
    import com.longteng.lesson2.dao.HelloWorld;
    
    public class HelloWorldImpl implements HelloWorld {
        @Override
        public void printHelloWorld() {
            System.out.println("HelloWorldImpl:printHelloWorld()------------");
        }
    
        @Override
        public void doPrint() {
            System.out.println("HelloWorldImpl:doPrint()------------");
        }
    }

    3、

    package com.longteng.lesson2.dao.impl;
    
    import com.longteng.lesson2.dao.HelloWorld;
    
    public class HelloWorldImpl1 implements HelloWorld {
        @Override
        public void printHelloWorld() {
            System.out.println("HelloWorldImpl1:printHelloWorld()------------");
        }
    
        @Override
        public void doPrint() {
            System.out.println("HelloWorldImpl1:doPrint()------------");
        }
    }

    4、

    package com.longteng.lesson2.service;
    
    public class TimeHandler {
        public void startTime()
        {
            System.out.println("CurrentTime = " + System.currentTimeMillis());
        }
        public void endTime()
        {
            try {
                Thread.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("EndTime = " + System.currentTimeMillis());
        }
    }

    5、

    <?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <bean id="helloWorldImpl" class="com.longteng.lesson2.dao.impl.HelloWorldImpl"></bean>
        <bean id="helloWorldImpl1" class="com.longteng.lesson2.dao.impl.HelloWorldImpl1"></bean>
        <bean id="timeHandler" class="com.longteng.lesson2.service.TimeHandler"> </bean>
        <aop:config>
            <aop:aspect id="time" ref="timeHandler" order="1">
                <aop:pointcut id="addTime" expression="execution(* com.longteng.lesson2.dao.impl.HelloWorldImpl*.*(..))" />
                <aop:before method="startTime" pointcut-ref="addTime" />
                <aop:after method="endTime" pointcut-ref="addTime" />
            </aop:aspect>
    
        </aop:config>
    
    </beans>

    6、

    package com.longteng.lesson2.dao;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AopTest {
        ApplicationContext context;
        @Before
        public void initApplication(){
            context = new ClassPathXmlApplicationContext("aop.xml");
        }
        @Test
        public void test(){
            HelloWorld helloWorld1 =(HelloWorld) context.getBean("helloWorldImpl");
            helloWorld1.doPrint();
            helloWorld1.printHelloWorld();
            System.out.println("----------------------------------------");
            HelloWorld helloWorld2 =(HelloWorld) context.getBean("helloWorldImpl1");
            helloWorld2.doPrint();
            helloWorld2.printHelloWorld();
        }
    }

    7、测试结果

    13:09:28.726 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldImpl'
    13:09:28.734 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    CurrentTime = 1544504968735
    HelloWorldImpl:doPrint()------------
    13:09:28.735 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    EndTime = 1544504968738
    13:09:28.738 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    CurrentTime = 1544504968738
    HelloWorldImpl:printHelloWorld()------------
    13:09:28.738 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    EndTime = 1544504968741
    ----------------------------------------
    13:09:28.741 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldImpl1'
    13:09:28.741 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    CurrentTime = 1544504968741
    HelloWorldImpl1:doPrint()------------
    13:09:28.741 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    EndTime = 1544504968745
    13:09:28.745 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    CurrentTime = 1544504968745
    HelloWorldImpl1:printHelloWorld()------------
    13:09:28.745 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'timeHandler'
    EndTime = 1544504968748
    
    Process finished with exit code 0
  • 相关阅读:
    谈论quick-cocos2d-x和cocos2d-x lua了解差异
    VirtualBox更改虚拟机磁盘VDI的大小
    HDU 1484 Basic wall maze (dfs + 记忆)
    CII-原子
    [Openstack] Expecting an auth URL via either --os-auth-url or env[OS_AUTH_URL]
    iOS安全攻防(三):使用Reveal分析他人app
    数据库索引的作用和长处缺点
    用EnableMenuItem不能使菜单变灰的原因
    Java设计模式-观察者模式
    IplImage 封装释放
  • 原文地址:https://www.cnblogs.com/zhou-test/p/10101566.html
Copyright © 2011-2022 走看看