AOP之切面与通知的一般写法

本文介绍AOP编程中的切面和通知概念,通过示例展示接口、实现类、切面类以及测试类的创建和配置,探讨如何在实际应用中设置切面的优先级。
摘要由CSDN通过智能技术生成

文件结构与jar包

接口类MathI

package com.atguigu.spring.aop;

public interface MathI {
	public int add(int i,int j);
	public int sub(int i,int j);
	public int mul(int i,int j);
	public int div(int i,int j);
	public void Descript();

}

接口实现类MathImpl

package com.atguigu.spring.aop;

import org.springframework.stereotype.Component;

@Component
public class MathImpl implements MathI {

	@Override
	public int add(int i, int j) {
		// TODO Auto-generated method stub
		int result=i+j;
		
		return result;

	}

	@Override
	public int sub(int i, int j) {
		// TODO Auto-generated method stub
		int result=i-j;
		return result;

	}

	@Override
	public int mul(int i, int j) {
		// TODO Auto-generated method stub
		int result=i*j;
		return result;

	}

	@Override
	public int div(int i, int j) {
		// TODO Auto-generated method stub
		int result=i/j;
		return result;

	}

	@Override
	public void Descript() {
		System.out.println("加减乘除");
		// TODO Auto-generated method stub
		
	}

}

切面类MyLoggerAspect

package com.atguigu.spring.aop;

import java.util.Arrays;

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;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component  //交于Spring管理
@Aspect    //标注当前类为切面
@Order(1)  //确定切面作用的优先级,值越小优先级越高  默认值为INT最大值
public class MyloggerAspect {
	//类里的方法叫做通知
	
	@Pointcut(value="execution(* com.atguigu.spring.aop.*.*(..))")
	public void test(){
		
	}
	/**
	 * @Before&#x
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值