一、横切关注点spring
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点express
二、切面(aspect)less
类是对物体特征的抽象,切面就是对横切关注点的抽象ide
三、链接点(joinpoint)测试
被拦截到的点,由于Spring只支持方法类型的链接点,因此在Spring中链接点指的就是被拦截到的方法,实际上链接点还能够是字段或者构造器spa
四、切入点(pointcut)代理
对链接点进行拦截的定义code
五、通知(advice)xml
所谓通知指的就是指拦截到链接点以后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类对象
六、目标对象
代理的目标对象
七、织入(weave)
将切面应用到目标对象并致使代理对象建立的过程
八、引入(introduction)
在不修改代码的前提下,引入能够在运行期为类动态地添加一些方法或字段
进入实例代码
一、
package com.longteng.lesson2.dao; import org.aspectj.lang.ProceedingJoinPoint; public interface HelloWorld { void printHelloWorld(); void doPrint(); }
二、
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()------------"); } }
三、
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()------------"); } }
四、
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()); } }
五、
<?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>
六、
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(); } }
七、测试结果
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