最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • 在Spring中使用JDBC和JDBC模板的講解

    在Spring中使用JDBC和JDBC模板的講解

    今天小編就為大家分享一篇關(guān)于在Spring中使用JDBC和JDBC模板的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • IDEA(2022.2)搭建Servlet基本框架超詳細(xì)步驟

    IDEA(2022.2)搭建Servlet基本框架超詳細(xì)步驟

    這篇文章主要給大家介紹了關(guān)于IDEA(2022.2)搭建Servlet基本框架超詳細(xì)步驟,Servlet容器負(fù)責(zé)Servlet和客戶的通信以及調(diào)用Servlet的方法,Servlet和客戶的通信采用"請(qǐng)求/響應(yīng)"的模式,需要的朋友可以參考下
    2023-10-10
  • 在Spring中配置Quartz的三種方式

    在Spring中配置Quartz的三種方式

    Spring Quartz 是一個(gè)任務(wù)調(diào)度框架,它允許我們定期執(zhí)行特定的任務(wù),在 Spring 中,我們可以通過多種方式來配置 Quartz,包括使用 ??@Scheduled?? 注解、XML 配置和 Java 配置,本文將介紹如何在 Spring 中使用這三種方式來配置 Quartz,需要的朋友可以參考下
    2025-01-01
  • Java如何把int類型轉(zhuǎn)換成byte

    Java如何把int類型轉(zhuǎn)換成byte

    這篇文章主要介紹了Java如何把int類型轉(zhuǎn)換成byte,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java SHA-256加密的兩種實(shí)現(xiàn)方法詳解

    Java SHA-256加密的兩種實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Java SHA-256加密的兩種實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)SHA-256加密的實(shí)現(xiàn)代碼與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-08-08
  • 解決打開的idea項(xiàng)目maven不生效問題

    解決打開的idea項(xiàng)目maven不生效問題

    這篇文章主要給大家介紹了關(guān)于如何解決打開的idea項(xiàng)目maven不生效問題,最近在配置maven時(shí),發(fā)現(xiàn)無論配置幾遍,IDEA中的maven配置總會(huì)還原成默認(rèn)的,所以這里給大家分享下解決辦法,需要的朋友可以參考下
    2023-07-07
  • mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的方法

    mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的方法

    這篇文章主要給大家介紹了關(guān)于mybatis攔截器實(shí)現(xiàn)通用權(quán)限字段添加的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 詳解如何在Spring MVC中處理AJAX請(qǐng)求

    詳解如何在Spring MVC中處理AJAX請(qǐng)求

    在現(xiàn)代 web 開發(fā)中,AJAX(Asynchronous JavaScript and XML)被廣泛用于創(chuàng)建響應(yīng)式和動(dòng)態(tài)的用戶界面,與傳統(tǒng)的頁面刷新不同,AJAX 允許網(wǎng)頁在不重新加載的情況下與服務(wù)器交換數(shù)據(jù),從而提升了用戶體驗(yàn),本篇博客將深入探討如何在 Spring MVC 中處理 AJAX 請(qǐng)求
    2024-11-11
  • SpringBoot?mybatis-plus使用json字段實(shí)戰(zhàn)指南

    SpringBoot?mybatis-plus使用json字段實(shí)戰(zhàn)指南

    在現(xiàn)代應(yīng)用開發(fā)中經(jīng)常會(huì)使用JSON格式存儲(chǔ)和傳輸數(shù)據(jù),為了便捷地處理數(shù)據(jù)庫中的JSON字段,MyBatis-Plus提供了強(qiáng)大的JSON處理器,這篇文章主要給大家介紹了關(guān)于SpringBoot?mybatis-plus使用json字段的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • java并發(fā)編程之進(jìn)程和線程調(diào)度基礎(chǔ)詳解

    java并發(fā)編程之進(jìn)程和線程調(diào)度基礎(chǔ)詳解

    這篇文章主要介紹了java并發(fā)編程之進(jìn)程和線程調(diào)度基礎(chǔ),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06

最新評(píng)論

远安县| 汤原县| 武平县| 普定县| 镇安县| 贵港市| 双流县| 金寨县| 阜康市| 拉萨市| 成安县| 津市市| 新安县| 满城县| 新郑市| 宜兰市| 和平区| 英吉沙县| 岳普湖县| 鹤山市| 田东县| 华蓥市| 峨山| 天台县| 那坡县| 罗山县| 邵阳市| 奉节县| 安陆市| 东乡| 宜阳县| 三河市| 玛纳斯县| 万年县| 内丘县| 伽师县| 怀安县| 镇平县| 巩留县| 平塘县| 新化县|