欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring AOP定義AfterThrowing增加處理實(shí)例分析

 更新時(shí)間:2020年01月08日 10:49:46   作者:cakincqm  
這篇文章主要介紹了spring AOP定義AfterThrowing增加處理,結(jié)合實(shí)例形式分析了spring面向切面AOP定義AfterThrowing相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下

本文實(shí)例講述了spring AOP定義AfterThrowing增加處理。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
   <!-- 指定自動(dòng)搜索Bean組件、自動(dòng)搜索切面類 -->
   <context:component-scan
      base-package="org.crazyit.app.service
      ,org.crazyit.app.aspect">
      <context:include-filter type="annotation"
        expression="org.aspectj.lang.annotation.Aspect" />
   </context:component-scan>
   <!-- 啟動(dòng)@AspectJ支持 -->
   <aop:aspectj-autoproxy />
</beans>

二 切面類

package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定義一個(gè)切面
@Aspect
public class RepairAspect
{
  // 匹配org.crazyit.app.service.impl包下所有類的、
  // 所有方法的執(zhí)行作為切入點(diǎn)
  @AfterThrowing(throwing="ex"
    , pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
  // 聲明ex時(shí)指定的類型會(huì)限制目標(biāo)方法必須拋出指定類型的異常
  // 此處將ex的類型聲明為Throwable,意味著對(duì)目標(biāo)方法拋出的異常不加限制
  public void doRecoveryActions(Throwable ex)
  {
    System.out.println("目標(biāo)方法中拋出的異常:" + ex);
    System.out.println("模擬Advice對(duì)異常的修復(fù)...");
  }
}

三 接口

Hello

package org.crazyit.app.service;
public interface Hello {
   // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
   void foo();
   // 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶的方法
   int addUser(String name, String pass);
}

World

package org.crazyit.app.service;
public interface World {
   // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
   public void bar();
}

四 實(shí)現(xiàn)類

HelloImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello
{
  // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void foo()
  {
    System.out.println("執(zhí)行Hello組件的foo()方法");
  }
  // 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶的方法
  public int addUser(String name , String pass)
  {
    System.out.println("執(zhí)行Hello組件的addUser添加用戶:" + name);
    if(name.length() < 3 || name.length() > 10)
    {
      throw new IllegalArgumentException("name參數(shù)的長(zhǎng)度必須大于3,小于10!");
    }
    return 20;
  }
}

WorldImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("world")
public class WorldImpl implements World {
  // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void bar() {
    System.out.println("執(zhí)行World組件的bar()方法");
  }
}

五 測(cè)試類

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest {
  public static void main(String[] args) {
    // 創(chuàng)建Spring容器
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    Hello hello = ctx.getBean("hello", Hello.class);
    hello.foo();
    hello.addUser("悟空", "7788");
    World world = ctx.getBean("world", World.class);
    world.bar();
  }
}

六 測(cè)試結(jié)果

執(zhí)行Hello組件的foo()方法
執(zhí)行Hello組件的addUser添加用戶:悟空
目標(biāo)方法中拋出的異常:java.lang.IllegalArgumentException: name參數(shù)的長(zhǎng)度必須大于3,小于10!
模擬Advice對(duì)異常的修復(fù)...
Exception in thread "main" java.lang.IllegalArgumentException:  name參數(shù)的長(zhǎng)度必須大于3,小于10!
     at  org.crazyit.app.service.impl.HelloImpl.addUser(HelloImpl.java:30)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native  Method)
     at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
     at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
     at java.lang.reflect.Method.invoke(Method.java:498)
     at  org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
     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:207)
     at com.sun.proxy.$Proxy7.addUser(Unknown Source)
     at lee.BeanTest.main(BeanTest.java:26)

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 基于Mybatis的配置文件入門必看篇

    基于Mybatis的配置文件入門必看篇

    這篇文章主要介紹了Mybatis的配置文件入門,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 解析JavaSe的抽象類和接口

    解析JavaSe的抽象類和接口

    這篇文章主要為大家詳細(xì)介紹了JavaSe的抽象類和接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java如何獲取數(shù)組和字符串的長(zhǎng)度(length還是length())

    Java如何獲取數(shù)組和字符串的長(zhǎng)度(length還是length())

    這篇文章主要介紹了Java如何獲取數(shù)組和字符串的長(zhǎng)度(length還是length()),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Java中實(shí)現(xiàn)用戶之間的通訊方式

    Java中實(shí)現(xiàn)用戶之間的通訊方式

    在Java中實(shí)現(xiàn)用戶間通訊主要有兩種方法:Socket編程和WebSocket,Socket編程允許兩個(gè)設(shè)備間進(jìn)行數(shù)據(jù)交換,適用于基本的網(wǎng)絡(luò)通訊,本文提供了兩種方法的基本實(shí)現(xiàn)代碼和相關(guān)配置,幫助開發(fā)者根據(jù)需求選擇合適的通訊方式
    2024-09-09
  • MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問題

    MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問題

    這篇文章主要介紹了MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • springboot集成ftp實(shí)現(xiàn)文件上傳

    springboot集成ftp實(shí)現(xiàn)文件上傳

    這篇文章主要為大家詳細(xì)介紹了springboot集成ftp實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Java面向?qū)ο笾饔糜蛟敿?xì)解讀

    Java面向?qū)ο笾饔糜蛟敿?xì)解讀

    這篇文章主要介紹了Java面向?qū)ο笾饔糜蛟敿?xì)解讀,在java編程中,主要的變量就是屬性和局部變量,局部變量一般是指在成員方法中定義的變量,局部變量也就是除了屬性之外的其他變量,作用域?yàn)槎x他的代碼塊中,需要的朋友可以參考下
    2024-01-01
  • Java 讀取外部資源的方法詳解及實(shí)例代碼

    Java 讀取外部資源的方法詳解及實(shí)例代碼

    這篇文章主要介紹了Java 讀取外部資源的方法詳解及實(shí)例代碼的相關(guān)資料,經(jīng)常有讀取外部資源的要求,如配置文件等等需要讀取,需要的朋友可以參考下
    2017-03-03
  • java開源好用的簡(jiǎn)繁轉(zhuǎn)換類庫(kù)推薦

    java開源好用的簡(jiǎn)繁轉(zhuǎn)換類庫(kù)推薦

    這篇文章主要為大家介紹了java開源好用的簡(jiǎn)繁轉(zhuǎn)換類庫(kù)推薦,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 淺析JVM垃圾回收的過程

    淺析JVM垃圾回收的過程

    這篇文章主要介紹了JVM垃圾回收的過程,幫助大家更好的理解和學(xué)習(xí)Java中的垃圾回收機(jī)制,感興趣的朋友可以了解下
    2020-09-09

最新評(píng)論