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

Spring AOP中定義切點的實現(xiàn)方法示例

 更新時間:2020年01月10日 10:38:22   作者:cakincqm  
這篇文章主要介紹了Spring AOP中定義切點的實現(xiàn)方法,結(jié)合實例形式分析了spring面向切面AOP定義切點的具體步驟、實現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了Spring AOP中定義切點的實現(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>

二 切面類

SystemArchitecture

package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
@Aspect
public class SystemArchitecture
{
   @Pointcut("execution(* org.crazyit.app.service.impl.*.*(..))")
   public void myPointcut(){}
}

LogAspect

package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
@Aspect
public class LogAspect
{
  // 直接使用SystemArchitecture切面類的myPointcut()切入點
  @AfterReturning(returning="rvt"
    , pointcut="SystemArchitecture.myPointcut()")
  // 聲明rvt時指定的類型會限制目標方法必須返回指定類型的值或沒有返回值
  // 此處將rvt的類型聲明為Object,意味著對目標方法的返回值不加限制
  public void log(Object rvt)
  {
    System.out.println("獲取目標方法返回值:" + 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();
}

四 實現(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);
    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ī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()方法
獲取目標方法返回值:null
模擬記錄日志功能...
執(zhí)行Hello組件的addUser添加用戶:孫悟空
獲取目標方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法
獲取目標方法返回值:null
模擬記錄日志功能...

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

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

相關(guān)文章

  • SpringBoot啟動流程SpringApplication準備階段源碼分析

    SpringBoot啟動流程SpringApplication準備階段源碼分析

    這篇文章主要為大家介紹了SpringBoot啟動流程SpringApplication準備階段源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • JDK10中的局部變量類型推斷var

    JDK10中的局部變量類型推斷var

    這篇文章主要介紹了JDK10中的局部變量類型推斷var,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Servlet從入門到精通(超級詳細!)

    Servlet從入門到精通(超級詳細!)

    在JavaWeb項目中,處理請求和發(fā)送響應(yīng)的過程是由一種叫做Servlet 的程序來完成的,并且 Servlet 是為了解決實現(xiàn)動態(tài)頁面而衍生的東西,下面這篇文章主要給大家介紹了關(guān)于Servlet從入門到精通的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • 一口氣說出Java 6種延時隊列的實現(xiàn)方法(面試官也得服)

    一口氣說出Java 6種延時隊列的實現(xiàn)方法(面試官也得服)

    這篇文章主要介紹了一口氣說出Java 6種延時隊列的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • java關(guān)鍵字static學(xué)習(xí)心得

    java關(guān)鍵字static學(xué)習(xí)心得

    本篇文章給大家分享一篇關(guān)于java關(guān)鍵字static的學(xué)習(xí)心得,有這方面需要的朋友學(xué)習(xí)下吧。
    2018-01-01
  • 解決Android Studio安裝后運行出錯dose not...和Internal error...

    解決Android Studio安裝后運行出錯dose not...和Internal error...

    這篇文章主要介紹了解決Android Studio安裝后運行出錯dose not...和Internal error...的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java編程中10個最佳的異常處理技巧

    Java編程中10個最佳的異常處理技巧

    這篇文章主要介紹了Java編程中10個最佳的異常處理技巧,在本文中,將討論Java異常處理最佳實踐,這些Java最佳實踐遵循標準的JDK庫,和幾個處理錯誤和異常的開源代碼,這還是一個提供給java程序員編寫健壯代碼的便利手冊,需要的朋友可以參考下
    2015-01-01
  • java 如何為文件及文件夾添加權(quán)限

    java 如何為文件及文件夾添加權(quán)限

    這篇文章主要介紹了java 如何為文件及文件夾添加權(quán)限的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題

    IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題

    這篇文章主要介紹了IDEA中l(wèi)og4j 無法輸出到本地 properties配置無效問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • Java基礎(chǔ)教程之理解Annotation詳細介紹

    Java基礎(chǔ)教程之理解Annotation詳細介紹

    這篇文章主要介紹了Java基礎(chǔ)教程之理解Annotation詳細介紹的相關(guān)資料,需要的朋友可以參考下
    2017-01-01

最新評論