初步使用AOP编写一个helloword的步骤以及相关知识的解析

1、首先建立一个接口,建立一个实现接口的实现类。

package com.test.spring.aop.impl;

import org.springframework.stereotype.Component;


 interface ArithmeticCalaculator {
	int add(int i,int j);
	int sub(int i,int j);
	int mul(int i,int j);
	int div(int i,int j);
}

package com.test.spring.aop.impl;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalaculator {
	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {
		int result = i - j;
		return result;
	}

	@Override
	public int mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i / j;
		return result;
	}

}

package com.test.spring.aop.impl;

import java.util.Arrays;

import javax.management.RuntimeErrorException;

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.springframework.stereotype.Component;

//此类是切面所以要加一个特殊注解
@Component
@Aspect
public class LoggingAspect {

	/*
	 * 在com.test.spring.aop.ArithmeticCalactulato接口每个实现类的每一个方法 使之前执行一段代码
	 * ArithmeticCalaculator 前置通知
	 */
	@Before("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
	public void beforemethod(JoinPoint joint) {
		String methodname = joint.getSignature().getName();
		Object[] args = joint.getArgs();
		System.out.println("the mehtod" + methodname + " begins with:"
				+ Arrays.asList(args));

	}

	/*
	 * 后置通知 在目标方法执行之后进行执行
	 */
	@After("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
	public void afterMethod(JoinPoint joint) {
		// 获取目标方法名称
		String methodname = joint.getSignature().getName();
		Object[] obj = joint.getArgs();
		System.out.println("the method:" + methodname + "  end with args"
				+ Arrays.asList(obj));
	}

	/*
	 * 有些方法是在目标方法正常执行之后执行。返回通知 返回通知是可以访问到方法的返回值的。
	 */
	@AfterReturning(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", returning = "result")
	public void afterReturn(JoinPoint point, Object result) {
		String mehtodname = point.getSignature().getName();
		System.out.println("the method------ bengin" + mehtodname
				+ " end with:" + result);

	}

	/*
	 * 在目标方法出现异常时候执行的代码 可以访问到异常;且可以指定异常的类型。 异常通知。如果目标方法中存在异常。这时候。会执行这个方法
	 */
	@AfterThrowing(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", throwing = "ex")
	public void afterThrowing(JoinPoint point, Exception ex) {
		String mehtodname = point.getSignature().getName();
		System.out.println("the method------ bengin" + mehtodname
				+ " occur with:" + ex);
	}

	/*
	 * Around环绕通知 环绕通知执行的方法需要在参数里使用ProcedingJoinPoin类型的参数
	 * 其中环绕通知类似于动态代理的全过程。就是ProcedingJoinPoint类型的参数决定是否执行目标通知
	 * 且环绕通知必须有返回值。返回值即为目标方法的返回值。
	 */
/*
 * 只做了解即可
 */
	@Around(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
	public Object aroundMethod(ProceedingJoinPoint pro) {
		// 执行目标方法
		Object result = null;
		String methodname = pro.getSignature().getName();
		try {
			// 前置通知
			System.out.println("Th==-=-=-start-=-=-= method" + methodname
					+ "begins with" + Arrays.asList(pro.getArgs()));
			result = pro.proceed();
			// 后置通知
			System.out.println("Th==-=-=-end=-=-=-= method" + methodname
					+ "end with" + result);
		} catch (Throwable e) {
			// 异常通知
			System.out.println("The method occurs exception" + e);
			throw new RuntimeErrorException((Error) e);
		}
		System.out.println(" the aroundMethod is" + methodname + "ends");
		return result;

	}
}


2、在实现类中我们为其采用注解加入ioc容器的配置方法。并且在该类的上部加入注解的形式为
@Component("arithmeticCalculator")
3.建立一个测试Aspect类。我们也为其加入到ioc容器中采用注解的方式进行注入。
同时为其加入特殊的切面注解类。
@Aspect
切面类完成之后我们可以在这个类里进行写方法在方法上边需要加上注解:常见的注解有五种形式
分别对应目标方法执行的流程的前后。
1.@Before   对目标方法的调用前的执行
@Before("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void beforemethod(JoinPoint joint) {
String methodname = joint.getSignature().getName();
Object[] args = joint.getArgs();
System.out.println("the mehtod" + methodname + " begins with:"
+ Arrays.asList(args));
}

2.@After    对目标方法的调用后的执行
@After("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void afterMethod(JoinPoint joint) {
// 获取目标方法名称
String methodname = joint.getSignature().getName();
Object[] obj = joint.getArgs();
System.out.println("the method:" + methodname + "  end with args"
+ Arrays.asList(obj));
}


3.@AfterReturning  有些方法是在目标方法正常执行之后执行需要返回通知 返回通知是可以访问到方法的返回值的。
/*
* 有些方法是在目标方法正常执行之后执行。返回通知 返回通知是可以访问到方法的返回值的。
*/
@AfterReturning(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", returning = "result")
public void afterReturn(JoinPoint point, Object result) {
String mehtodname = point.getSignature().getName();
System.out.println("the method------ bengin" + mehtodname+ " end with:" + result);
}
4.@AfterThrowing   在目标方法出现异常时候执行的代码 可以访问到异常;且可以指定异常的类型。 异常通知。如果目标方法中存在异常。这时候。会执行这个方法
@AfterThrowing(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))", throwing = "ex")
public void afterThrowing(JoinPoint point, Exception ex) {
String mehtodname = point.getSignature().getName();
System.out.println("the method------ bengin" + mehtodname
+ " occur with:" + ex);
}
5.@Around    /*
* Around环绕通知 环绕通知执行的方法需要在参数里使用ProcedingJoinPoin类型的参数
* 其中环绕通知类似于动态代理的全过程。就是ProcedingJoinPoint类型的参数决定是否执行目标通知
* 且环绕通知必须有返回值。返回值即为目标方法的返回值。
*/
@Around(value = "execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pro) {
// 执行目标方法
Object result = null;
String methodname = pro.getSignature().getName();
try {
// 前置通知
System.out.println("Th==-=-=-start-=-=-= method" + methodname
+ "begins with" + Arrays.asList(pro.getArgs()));
result = pro.proceed();
// 后置通知
System.out.println("Th==-=-=-end=-=-=-= method" + methodname
+ "end with" + result);
} catch (Throwable e) {
// 异常通知
System.out.println("The method occurs exception" + e);
throw new RuntimeErrorException((Error) e);
}
System.out.println(" the aroundMethod is" + methodname + "ends");
return result;


}
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uniquewdl

匆忙的人生,总有你喜欢的文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值