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

spring AOP的After增強實現(xiàn)方法實例分析

 更新時間:2020年01月08日 11:11:56   作者:cakincqm  
這篇文章主要介紹了spring AOP的After增強實現(xiàn)方法,結合實例形式分析了spring面向切面AOP的After增強實現(xiàn)步驟與相關操作技巧,需要的朋友可以參考下

本文實例講述了spring AOP的After增強實現(xiàn)方法。分享給大家供大家參考,具體如下:

一 配置

<?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">
   <!-- 指定自動搜索Bean組件、自動搜索切面類 -->
   <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>
   <!-- 啟動@AspectJ支持 -->
   <aop:aspectj-autoproxy />
</beans>

二 切面類

package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定義一個切面
@Aspect
public class ReleaseAspect
{
  // 匹配org.crazyit.app.service包下所有類的、
  // 所有方法的執(zhí)行作為切入點
  @After("execution(* org.crazyit.app.service.*.*(..))")
  public void release()
  {
    System.out.println("模擬方法結束后的釋放資源...");
  }
}

三 接口

Hello

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

World

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

四 實現(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
{
  // 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法
  public void foo()
  {
    System.out.println("執(zhí)行Hello組件的foo()方法");
  }
  // 定義一個addUser()方法,模擬應用中的添加用戶的方法
  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ù)的長度必須大于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 {
  // 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法
  public void bar() {
    System.out.println("執(zhí)行World組件的bar()方法");
  }
}

五 測試類

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();
  }
}

六 測試結果

執(zhí)行Hello組件的foo()方法
模擬方法結束后的釋放資源...
執(zhí)行Hello組件的addUser添加用戶:悟空
模擬方法結束后的釋放資源...
Exception in thread "main" java.lang.IllegalArgumentException:  name參數(shù)的長度必須大于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.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
     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)

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

希望本文所述對大家java程序設計有所幫助。

相關文章

  • SpringBoot3集成Swagger3的詳細教程

    SpringBoot3集成Swagger3的詳細教程

    Swagger 3(OpenAPI 3.0)提供了更加強大和靈活的API文檔生成能力,本教程將指導您如何在Spring Boot 3項目中集成Swagger3,并使用Knife4j作為UI界面,需要的朋友可以參考下
    2024-03-03
  • MyBatis核心配置文件深入分析

    MyBatis核心配置文件深入分析

    這篇文章主要介紹了MyBatis核心配置文件,MyBatis的前身就是iBatis,iBatis本是由Clinton Begin開發(fā),后來捐給Apache基金會,成立了iBatis開源項目。2010年5月該項目由Apahce基金會遷移到了Google Code,并且改名為MyBatis
    2022-12-12
  • maven profile動態(tài)選擇配置文件詳解

    maven profile動態(tài)選擇配置文件詳解

    這篇文章主要介紹了maven profile動態(tài)選擇配置文件詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • 使用java一維數(shù)組模擬壓棧彈棧

    使用java一維數(shù)組模擬壓棧彈棧

    這篇文章主要介紹了如何使用java一維數(shù)組模擬壓棧彈棧,需要的朋友可以參考下
    2021-04-04
  • ChatGPT介紹及Java?API調用

    ChatGPT介紹及Java?API調用

    本文主要介紹了ChatGPT介紹及Java?API調用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • Java中對象快速復制的幾種方式詳解

    Java中對象快速復制的幾種方式詳解

    這篇文章主要介紹了Java中對象快速復制的幾種方式詳解,對象的克隆是指創(chuàng)建一個新的對象,且新的對象的狀態(tài)與原始對象的狀態(tài)相同,當對克隆的新對象進行修改時,不會影響原始對象的狀態(tài),需要的朋友可以參考下
    2023-08-08
  • JDBC實現(xiàn)數(shù)據(jù)庫增刪改查功能

    JDBC實現(xiàn)數(shù)據(jù)庫增刪改查功能

    這篇文章主要為大家詳細介紹了JDBC實現(xiàn)數(shù)據(jù)庫增刪改查功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 詳解Servlet 3.0/3.1 中的異步處理

    詳解Servlet 3.0/3.1 中的異步處理

    這篇文章主要介紹了詳解Servlet 3.0/3.1 中的異步處理,實例分析了servlet 3.0異步處理的技巧,非常具有實用價值,需要的朋友可以參考下
    2017-04-04
  • Java持久層框架MyBatis簡單實例

    Java持久層框架MyBatis簡單實例

    MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,并且改名為MyBatis 。本文重點給大家介紹Java持久層框架MyBatis簡單實例,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧
    2016-07-07
  • Java使用Spring發(fā)送郵件的實現(xiàn)代碼

    Java使用Spring發(fā)送郵件的實現(xiàn)代碼

    本篇文章主要介紹了使用Spring發(fā)送郵件的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03

最新評論