一、橫切關注點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