亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring AOP定義AfterReturning增加實例分析

 更新時間:2020年01月07日 08:44:11   作者:cakincqm  
這篇文章主要介紹了Spring AOP定義AfterReturning增加,結合實例形式分析了Spring面相切面AOP定義AfterReturning增加相關操作技巧與使用注意事項,需要的朋友可以參考下

本文實例講述了Spring AOP定義AfterReturning增加。分享給大家供大家參考,具體如下:

一 配置

<?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 LogAspect
{
  // 匹配org.crazyit.app.service.impl包下所有類的、
  // 所有方法的執(zhí)行作為切入點
  @AfterReturning(returning="rvt"
    , pointcut="execution(* org.crazyit.app.service.impl.*.*(..))")
  // 聲明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è)務邏輯方法
   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()方法
獲取目標方法返回值:null
模擬記錄日志功能...
執(zhí)行Hello組件的addUser添加用戶:孫悟空
獲取目標方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法
獲取目標方法返回值:null
模擬記錄日志功能...

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

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

相關文章

  • Spring通過Java配置集成Tomcat的方法

    Spring通過Java配置集成Tomcat的方法

    這篇文章主要介紹了Spring通過Java配置集成Tomcat的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Java.SE數(shù)組的一些常見練習題

    Java.SE數(shù)組的一些常見練習題

    數(shù)組可以看成是相同類型元素的一個集合,在內存中是一段連續(xù)的空間,這篇文章主要給大家介紹了關于Java.SE數(shù)組的一些常見練習題,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • JavaWeb頁面中防止點擊Backspace網(wǎng)頁后退情況

    JavaWeb頁面中防止點擊Backspace網(wǎng)頁后退情況

    當鍵盤敲下后退鍵(Backspace)后怎么防止網(wǎng)頁后退情況呢?今天小編通過本文給大家詳細介紹下,感興趣的朋友一起看看吧
    2016-11-11
  • Spring中@EnableScheduling實現(xiàn)定時任務代碼實例

    Spring中@EnableScheduling實現(xiàn)定時任務代碼實例

    這篇文章主要介紹了Spring中@EnableScheduling實現(xiàn)定時任務代碼實例,@EnableScheduling 注解開啟定時任務功能,可以將多個方法寫在一個類,也可以分多個類寫,當然也可以將方法直接寫在上面ScheddulConfig類中,需要的朋友可以參考下
    2024-01-01
  • Java實現(xiàn)順序表的增刪查改功能

    Java實現(xiàn)順序表的增刪查改功能

    這篇文章主要介紹了Java實現(xiàn)順序表的增刪查改功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Spring實戰(zhàn)之Bean銷毀之前的行為操作示例

    Spring實戰(zhàn)之Bean銷毀之前的行為操作示例

    這篇文章主要介紹了Spring實戰(zhàn)之Bean銷毀之前的行為操作,結合實例形式分析了spring在bean銷毀之前的行為相關設置與使用技巧,需要的朋友可以參考下
    2019-11-11
  • SpringBoot中JPA實現(xiàn)Sort排序的三種方式小結

    SpringBoot中JPA實現(xiàn)Sort排序的三種方式小結

    這篇文章主要介紹了SpringBoot中JPA實現(xiàn)Sort排序的三種方式小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 詳解如何在Java中實現(xiàn)堆排序算法

    詳解如何在Java中實現(xiàn)堆排序算法

    這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)堆排序算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • MyBatis的動態(tài)攔截sql并修改

    MyBatis的動態(tài)攔截sql并修改

    因工作需求,需要根據(jù)用戶的數(shù)據(jù)權限,來查詢并展示相應的數(shù)據(jù),那么就需要動態(tài)攔截sql,本文就來介紹了MyBatis的動態(tài)攔截sql并修改,感興趣的可以了解一下
    2023-11-11
  • Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法

    Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法

    這篇文章主要介紹了Java并發(fā)編程中使用Executors類創(chuàng)建和管理線程的用法,文中舉了用其啟動線程和設置線程優(yōu)先級的例子,需要的朋友可以參考下
    2016-03-03

最新評論