AspectJ 基于注解的实现各种通知

62 篇文章 1 订阅
60 篇文章 0 订阅

接口

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);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值