Spring框架學習——AOP的開發

一.AOP開發中的相關術語。html

  ——JoinPoint(鏈接點):指那些能夠被攔截到的點。好比增刪改查方法均可以加強,這些方法就能夠被稱爲是鏈接點。java

  ——PointCut:切入點,真正被攔截的點,指對哪些鏈接點進行攔截的定義。JoinPoint是指方法能夠加強,而切入點就是實際哪一個方法進行加強或修改,這樣的方法被稱爲切入點。web

  ——Advice(加強/通知):方法層面的加強。指攔截到了方法後須要作的事情,也就是加強的功能部分被稱爲通知。分爲前置(方法主體以前)、後置、環繞通知。 spring

  ——Introduction(引介):是一種特殊的通知,再不修改類代碼的前提下進行類層面的加強。express

  ——Target:代理的目標對象,在AOP中也能夠說是被加強的對象,如對UserDao加強,那麼UserDao就是目標對象。app

  ——Weaving(織入):指將通知(Advice)應用到目標(Target)的過程。 也就是將加強應用到目標對象來建立新的代理對象的過程。Spring採用動態織入的方式。eclipse

  ——Proxy(代理):一個類被AOP織入加強後,就產生一個結果代理類。ide

  ——Aspect(切面):是多個切入點和多個通知(引介)的結合。在代理中可能有切面。性能

 

2、Spring使用AspectJ進行AOP開發(XML方式單元測試

  A.建立動態web項目,引入jar包。

    spring核心開發包。

    Spring的AOP開發包。spring-aop-4.2.4.RELEASE.jar         com.springsource.org.aopalliance-1.0.0.jar

    aspectJ的開發包:com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar           spring-aspects-4.2.4.RELEASE.jar

    

 

  B.配置文件(如今開發AOP,因此要引入AOP的xml規範)

  這些xml規範不用記,在spring-framework-4.2.4.RELEASE-dist\docs\spring-framework-reference\html\xsd-configuration.html中就能夠找到各類約束。

<?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" 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"> <!-- bean definitions here -->


</beans>

 

   C.建立目標類進行配置  

 
 

  public interface OrderDao {

    void findAll();

    void remove();

    void save();

    void update();
  }

 
 

  public class OrderDaoImpl implements OrderDao {

    @Override
    public void findAll() {
      System.out.println("查找全部訂單......");
    }

    @Override
    public void remove() {
      System.out.println("移除訂單......");
    }

    @Override
    public void save() {
      System.out.println("保存客戶訂單......");
    }

    @Override
    public void update() {
      System.out.println("修改訂單信息......");
    }

}

 
<!-- 對目標類進行代理,交給spring管理 -->
    <bean id="orderDao" class="cn.xxx.spring4.demo1.OrderDaoImpl"></bean>

 

 D.編寫測試類。

  

//Spring與Junit整合的jar,用來單元測試,在別的地方也能夠用到,可是其餘地方須要開啓spring註解才能用,contextConfiguration是用來加載配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/config/applicationContext1.xml")
public class demo1 {
    
//    爲屬性設置值
    @Resource(name="orderDao")
    private OrderDao orderDao;
    
    @Test
    public void test1() {
        orderDao.findAll();
    }
}

測試結果:

18:04:16,743  INFO DefaultTestContextBootstrapper:259 - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
18:04:16,747  INFO DefaultTestContextBootstrapper:207 - Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
18:04:16,749  INFO DefaultTestContextBootstrapper:207 - Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/web/context/request/RequestAttributes]
18:04:16,750  INFO DefaultTestContextBootstrapper:207 - Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
18:04:16,751  INFO DefaultTestContextBootstrapper:185 - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@13c78c0b, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@12843fce, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3dd3bcd]
18:04:16,814  INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [config/applicationContext1.xml]
18:04:16,881  INFO GenericApplicationContext:578 - Refreshing org.springframework.context.support.GenericApplicationContext@59fa1d9b: startup date [Fri May 10 18:04:16 CST 2019]; root of context hierarchy
查找全部訂單......
18:04:16,940  INFO GenericApplicationContext:960 - Closing org.springframework.context.support.GenericApplicationContext@59fa1d9b: startup date [Fri May 10 18:04:16 CST 2019]; root of context hierarchy

 

E.通知類型

  前置通知 :在目標方法執行以前執行.

  後置通知 :在目標方法執行以後執行

  環繞通知 :在目標方法執行前和執行後執行

  異常拋出通知:在目標方法執行出現異常的時候執行

  最終通知 :不管目標方法是否出現異常最終通知都會執行.至關於final

演示:建立一個切面類(指的是該類能夠橫向切入一個鏈接點),好比權限校驗方法。在建立以前咱們先了解一下切入點表達式,所謂切入點表達式經過一些特色的表達式,Spring底層經過反射來將切入的點(具體要加強的方法)的全路徑+方法名與表達式一一對應、

  切入點表達式:execution(表達式) 

    表達式意義:      []表明能夠省略 ,因爲方法參數通常不知道,因此用..表示  execution表明執行

    [方法訪問修飾符] 方法返回值 包名.類名.方法名(方法的參數) 

    public * cn.itcast.spring.dao.*.*(..)   * 表明任意的意思,這裏表明方法返回值爲全部,類名和方法名任意,也就是找cn.itcast.spring.dao包下面的全部類中的方法,都被加強
  * cn.itcast.spring.dao.*.*(..) 
  * cn.itcast.spring.dao.UserDao+.*(..) 當前UserDao類及其子類的全部方法
  * cn.itcast.spring.dao..*.*(..) 當前包及其子包下的類的全部方法

切面類:

 
 

public class MyAspect {
  public void validate() {
    System.out.println("權限校驗");
  }

  public void logWrite(Object result) {
    System.out.println("日誌記錄");
  }
}

 

前置通知 

<!--配置 -->
<!-- 對目標類進行代理,交給spring管理 -->
    <bean id="orderDao" class="cn.xxx.spring4.demo1.OrderDaoImpl"></bean>
    <bean id="myAspect" class="cn.xxx.spring4.demo1.MyAspect"></bean>
    
    <aop:config>
        <aop:pointcut expression="execution (* cn.xxx.spring4.demo1.OrderDaoImpl.findAll(..))" id="pointcut1"/>
        <aop:aspect ref="myAspect">
       <!-- 前置通知 --> <aop:before method="validate" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config>

運行結果:

權限校驗
查找全部訂單......

後置通知

<aop:pointcut expression="execution(* cn.xxx.spring4.demo1.OrderDaoImpl.remove(..))" id="pointcut2"/>

    <!-- 後置通知,這裏面returning的返回值和切面類中的切入點方法裏面的參數一致就行, -->    
            <aop:after-returning method="logWrite" pointcut-ref="pointcut2" returning="result"/>

運行結果:

移除訂單......
日誌記錄

 

環繞通知

切面類方法:

public void check(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("開啓性能監控");
//        執行切入點的目標程序,在方法先後插入,那麼中間的本來方法必定要執行,這句話就是執行原有方法的代碼
        joinPoint.proceed();
        System.out.println("結束性能監控");
    }

配置:

<aop:pointcut expression="execution(* cn.xxx.spring4.demo1.OrderDaoImpl.update(..))" id="pointcut3"/>

<aop:aspect ref="myAspect">
        <!-- 環繞通知 -->
            <aop:around method="check" pointcut-ref="pointcut3"/>
        </aop:aspect>

運行結果:

開啓性能監控
修改訂單信息......
結束性能監控

異常拋出通知

  切面類方法:

方法參數爲Throwable ex,異常對象,和配置中保持一致
public
void afterThrowing(Throwable ex) { ex.printStackTrace();
     system.out.println("發生除0異常") }

配置:

<!-- 異常拋出通知,注意這裏的throwing屬性的值必定和切面類方法裏面的參數一致 -->
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="ex"/>
        </aop:aspect>

若是切入點方法裏面出現異常,通知就會生效,就會執行通知的方法。

通知方法(切面類的方法)

public void afterThrowing(Throwable ex) {
        ex.printStackTrace();
        System.out.println("發生除0異常");
    }

 

運行結果:

java.lang.ArithmeticException: / by zero
    at cn.xxx.spring4.demo1.OrderDaoImpl.save(OrderDaoImpl.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy13.save(Unknown Source)
    at cn.xxx.test.demo1.testSave(demo1.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
發生除0異常

最終通知:和上面相似就不演示了。

相關文章
相關標籤/搜索