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