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

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

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

三 接口

Hello

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

World

package org.crazyit.app.service;
public interface World {
   // 定義一個簡單方法,模擬應(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 {
  // 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void foo() {
    System.out.println("執(zhí)行Hello組件的foo()方法");
  }
  // 定義一個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 {
  // 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  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();
  }
}

六 測試結(jié)果

執(zhí)行Hello組件的foo()方法
執(zhí)行Hello組件的addUser添加用戶:孫悟空
調(diào)用目標(biāo)方法第1個參數(shù)為:孫悟空
調(diào)用目標(biāo)方法第2個參數(shù)為:7788
獲取目標(biāo)方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法

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

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

相關(guān)文章

最新評論

丰台区| 佛坪县| 华安县| 合水县| 乐东| 达拉特旗| 合川市| 金阳县| 灵宝市| 盐源县| 东辽县| 河北区| 平顺县| 镇远县| 新泰市| 汕尾市| 监利县| 郎溪县| 芜湖市| 新巴尔虎左旗| 永德县| 库尔勒市| 汉阴县| 乡城县| 文昌市| 会东县| 开远市| 吉林省| 满洲里市| 铜山县| 紫云| 罗平县| 厦门市| 五大连池市| 乌兰浩特市| 拉萨市| 六安市| 田阳县| 海口市| 宝兴县| 嵊州市|