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

Spring AOP中使用args表達式的方法示例

 更新時間:2020年01月10日 10:24:40   作者:cakincqm  
這篇文章主要介紹了Spring AOP中使用args表達式的方法,結合實例形式分析了spring面向切面AOP中使用args表達式具體步驟、相關實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下

本文實例講述了Spring AOP中使用args表達式的方法。分享給大家供大家參考,具體如下:

一 配置

<?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 AccessArgAspect
{
  // 下面的args(arg0,arg1)會限制目標方法必須有2個形參
  @AfterReturning(returning="rvt" , pointcut=
    "execution(* org.crazyit.app.service.impl.*.*(..)) && args(arg0,arg1)")
  // 此處指定arg0、arg1為String類型
  // 則args(arg0,arg1)還要求目標方法的兩個形參都是String類型
  public void access(Object rvt, String arg0 , String arg1)
  {
    System.out.println("調用目標方法第1個參數(shù)為:" + arg0);
    System.out.println("調用目標方法第2個參數(shù)為:" + arg1);
    System.out.println("獲取目標方法返回值:" + rvt);
    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);
    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添加用戶:孫悟空
調用目標方法第1個參數(shù)為:孫悟空
調用目標方法第2個參數(shù)為:7788
獲取目標方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法

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

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

相關文章

  • Java多線程編程小實例模擬停車場系統(tǒng)

    Java多線程編程小實例模擬停車場系統(tǒng)

    這是一個關于Java多線程編程的例子,用多線程的思想模擬停車場管理系統(tǒng),這里分享給大家,供需要的朋友參考。
    2017-10-10
  • Java多線程 自定義線程池詳情

    Java多線程 自定義線程池詳情

    這篇文章主要介紹了Java多線程 自定義線程池,文章主要是學習代碼,沒有過多解析,需要的朋友可以參考一下文章的具體內容
    2021-10-10
  • 全面剖析java 數(shù)據(jù)類型與運算符

    全面剖析java 數(shù)據(jù)類型與運算符

    這篇文章主要介紹了Java基本數(shù)據(jù)類型和運算符,結合實例形式詳細分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉換、算術運算符、邏輯運算符等相關原理與操作技巧,需要的朋友可以參考下
    2021-09-09
  • SpringCloud使用Ribbon實現(xiàn)負載均衡的流程步驟

    SpringCloud使用Ribbon實現(xiàn)負載均衡的流程步驟

    在微服務架構中,負載均衡是一項關鍵的技術,它可以確保各個服務節(jié)點間的負載分布均勻,提高整個系統(tǒng)的穩(wěn)定性和性能,Spring Cloud 中的 Ribbon 就是一種負載均衡的解決方案,本文將深入探討 Ribbon 的原理和在微服務中的應用,需要的朋友可以參考下
    2024-02-02
  • Java 數(shù)組聲明、創(chuàng)建、初始化詳解

    Java 數(shù)組聲明、創(chuàng)建、初始化詳解

    本文主要介紹Java 數(shù)組聲明、創(chuàng)建、初始化的資料,這里整理相關知識,及簡單實現(xiàn)代碼,幫助大家學習,有興趣的小伙伴可以參考下
    2016-09-09
  • Java8新特性之StampedLock_動力節(jié)點Java學院整理

    Java8新特性之StampedLock_動力節(jié)點Java學院整理

    本文從synchronized、Lock到Java8新增的StampedLock進行對比分析,對Java8新特性之StampedLock相關知識感興趣的朋友一起看看吧
    2017-06-06
  • 最全LocalDateTime、LocalDate、Date、String相互轉化的方法

    最全LocalDateTime、LocalDate、Date、String相互轉化的方法

    大家在開發(fā)過程中必不可少的和日期打交道,對接別的系統(tǒng)時,時間日期格式不一致,每次都要轉化,本文為大家準備了最全的LocalDateTime、LocalDate、Date、String相互轉化方法,需要的可以參考一下
    2023-06-06
  • Spring Cloud學習教程之Zuul統(tǒng)一異常處理與回退

    Spring Cloud學習教程之Zuul統(tǒng)一異常處理與回退

    Spring Cloud Zuul對異常的處理整體來說還是比較方便的,流程也比較清晰,下面這篇文章主要給大家介紹了關于Spring Cloud學習教程之Zuul統(tǒng)一異常處理與回退的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2018-04-04
  • 史上最簡單的MyBatis動態(tài)SQL入門示例代碼

    史上最簡單的MyBatis動態(tài)SQL入門示例代碼

    動態(tài)sql,可以根據(jù)用戶對字段選擇和輸入,動態(tài)生成一條sql執(zhí)行。接下來通過本文給大家分享MyBatis動態(tài)SQL入門示例代碼,一起看看吧
    2017-03-03
  • SpringBoot整合tkMapper的方法

    SpringBoot整合tkMapper的方法

    項目使用SpringBoot2.0,H2數(shù)據(jù)庫,使用了?Lombok?簡化代碼,下面是本人使用SpringBoot整合tkMapper的一個小demo,記錄下來本人在此處踩得坑
    2022-11-11

最新評論