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

Spring AOP中使用args表達(dá)式的方法示例

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

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

一 配置

<?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.*;
@Aspect
public class AccessArgAspect
{
  // 下面的args(arg0,arg1)會(huì)限制目標(biāo)方法必須有2個(gè)形參
  @AfterReturning(returning="rvt" , pointcut=
    "execution(* org.crazyit.app.service.impl.*.*(..)) && args(arg0,arg1)")
  // 此處指定arg0、arg1為String類型
  // 則args(arg0,arg1)還要求目標(biāo)方法的兩個(gè)形參都是String類型
  public void access(Object rvt, String arg0 , String arg1)
  {
    System.out.println("調(diào)用目標(biāo)方法第1個(gè)參數(shù)為:" + arg0);
    System.out.println("調(diào)用目標(biāo)方法第2個(gè)參數(shù)為:" + arg1);
    System.out.println("獲取目標(biāo)方法返回值:" + rvt);
    System.out.println("模擬記錄日志功能...");
  }
}

三 接口

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);
    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添加用戶:孫悟空
調(diào)用目標(biāo)方法第1個(gè)參數(shù)為:孫悟空
調(diào)用目標(biāo)方法第2個(gè)參數(shù)為:7788
獲取目標(biāo)方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

    Java8新特性之StampedLock_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

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

    最全LocalDateTime、LocalDate、Date、String相互轉(zhuǎn)化的方法

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

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

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

    史上最簡(jiǎn)單的MyBatis動(dòng)態(tài)SQL入門(mén)示例代碼

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

    SpringBoot整合tkMapper的方法

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

最新評(píng)論