1. 程式人生 > >AspectJ 基於註解的實現各種通知

AspectJ 基於註解的實現各種通知

介面

package com.hk.spring.annotation;

public interface ISomeService {
    public void doFirst();
    public void doSecond();
    public String doThird();

}

實現介面

package com.hk.spring.annotation;

public class ISomeServiceImpl implements ISomeService {

    @Override
    public void doFirst() {
        System.out.println("主業務邏輯doFirst執行"
); } @Override public void doSecond() { System.out.println("主業務邏輯doSecond執行"); } @Override public String doThird() { System.out.println("主業務邏輯doThird執行"); return "ABC"; } }

切面類

package com.hk.spring.annotation;

import org.aspectj.lang.JoinPoint;
import
org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 定義一個簡單的POJO類作為切面類 * @author
浪丶蕩 * @param <throwing> * */
@Aspect //表示當前類為切面類 public class MyAspect<throwing> { /** * 前置通知 */ @Before("execution(* *..ISomeService.doFirst(..))") //表示前置通知的切入點表示式 public void before(){ System.out.println("前置方法"); } /** * 前置通知2 */ @Before("execution(* *..ISomeService.doThird(..))") public void before(JoinPoint jp){//jp為切入點表示式值 System.out.println("前置方法執行 "+jp); } /** * 後置通知 */ @AfterReturning("execution(* *..ISomeService.doSecond(..))") public void afterReturing(JoinPoint jp){//jp為切入點表示式值 System.out.println("後置方法執行 "+jp); } /** * 後置通知2 */ @AfterReturning(value="execution(* *..ISomeService.doThird(..))",returning="result") public void afterReturing(String result){ System.out.println("後置方法執行 result="+result); } /** * 環繞通知 */ @Around(value="execution(* *..ISomeService.doThird(..))") public String around(ProceedingJoinPoint p){ Object result = null; System.out.println("目標方法執行前"); try { //執行目標方法 result = p.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("目標方法執行後"); return ((String) result).toLowerCase(); } /** * 異常通知 */ @AfterThrowing(value="execution(* *..ISomeService.doSecond(..))",throwing="ex") public void afterThrowing(Exception ex){ System.out.println("afterThrowing執行,異常是:"+ex.getMessage()); } /** * 最終通知 */ @After("execution(* *..ISomeService.doSecond(..))") public void after(){ System.out.println("最終通知方法"); } }

配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 註冊目標物件 -->
        <bean id="someService" class="com.hk.spring.annotation.ISomeServiceImpl"></bean>
        <!-- 註冊切面 -->
        <bean id="myAspect" class="com.hk.spring.annotation.MyAspect"></bean>
        <!-- 註冊Aspect的自動代理物件 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

測試

package com.hk.spring.annotation;


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {

    @Test
    public void test1(){
        String resoure = "com/hk/spring/annotation/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resoure);
        ISomeService service = (ISomeService) ac.getBean("someService");
        service.doFirst();
        service.doSecond();
        String result = service.doThird();
        System.out.println(result);

    }
}