1. 程式人生 > 程式設計 >spring AOP的Around增強實現方法分析

spring AOP的Around增強實現方法分析

本文例項講述了spring AOP的Around增強實現方法。分享給大家供大家參考,具體如下:

一 配置

<?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 TxAspect
{
  // 匹配org.crazyit.app.service.impl包下所有類的、
  // 所有方法的執行作為切入點
  @Around("execution(* org.crazyit.app.service.impl.*.*(..))")
  public Object processTx(ProceedingJoinPoint jp)
    throws java.lang.Throwable
  {
    System.out.println("執行目標方法之前,模擬開始事務...");
    // 獲取目標方法原始的呼叫引數
    Object[] args = jp.getArgs();
    if(args != null && args.length > 1)
    {
      // 修改目標方法的第一個引數
      args[0] = "【增加的字首】" + args[0];
    }
    // 以改變後的引數去執行目標方法,並儲存目標方法執行後的返回值
    Object rvt = jp.proceed(args);
    System.out.println("執行目標方法之後,模擬結束事務...");
    // 如果rvt的型別是Integer,將rvt改為它的平方
    if(rvt != null && rvt instanceof Integer)
      rvt = (Integer)rvt * (Integer)rvt;
    return rvt;
  }
}

三 介面

Hello

package org.crazyit.app.service;
public interface Hello {
   // 定義一個簡單方法,模擬應用中的業務邏輯方法
   void foo();
   // 定義一個addUser()方法,模擬應用中的新增使用者的方法
   int addUser(String name,String pass);
}

World

package org.crazyit.app.service;
public interface World {
   // 定義一個簡單方法,模擬應用中的業務邏輯方法
   public void bar();
}

四 實現類

HelloImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello {
  // 定義一個簡單方法,模擬應用中的業務邏輯方法
  public void foo() {
    System.out.println("執行Hello元件的foo()方法");
  }
  // 定義一個addUser()方法,模擬應用中的新增使用者的方法
  public int addUser(String name,String pass) {
    System.out.println("執行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 {
  // 定義一個簡單方法,模擬應用中的業務邏輯方法
  public void bar() {
    System.out.println("執行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) {
    // 建立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();
  }
}

六 測試結果

執行目標方法之前,模擬開始事務...
執行Hello元件的foo()方法
執行目標方法之後,模擬結束事務...
執行目標方法之前,模擬開始事務...
執行Hello元件的addUser新增使用者:【增加的字首】孫悟空
執行目標方法之後,模擬結束事務...
addUser()的返回值為:400
執行目標方法之前,模擬開始事務...
執行World元件的bar()方法
執行目標方法之後,模擬結束事務...

更多關於java相關內容感興趣的讀者可檢視本站專題:《Spring框架入門與進階教程》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧彙總》和《Java快取操作技巧彙總》

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