博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringAOP之helloworld
阅读量:6993 次
发布时间:2019-06-27

本文共 3875 字,大约阅读时间需要 12 分钟。

hot3.png

新建javaProject,导入相关jar包

com.springsource.net.sf.cglib-2.2.0.jar

com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar

新建一个类HelloWorld.java

package cn.baokx.spring.aop.helloworld;import org.springframework.stereotype.Component;@Componentpublic class HelloWorld {	public int divide(int i,int j){		return i/j; 	}}

添加Spring配制文件applicationContext.xml

新建一个切面LoggingAspect.java

package cn.baokx.spring.aop.helloworld;import java.util.Arrays;import java.util.List;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;//把类交由IOC容器管理且声明为一个切面@Component@Aspect//切面的优先级,值越小,优先级越高 @Order(1)public class LoggingAspect {		//切入点表达式	@Pointcut("execution(public int cn.baokx.spring.aop.helloworld.HelloWorld.divide(int,int))")	public void declareJointPointExpression(){}		//声明为前置通知:在目标方法开始之前执行	@Before("declareJointPointExpression()")	public void beforeMethod(JoinPoint joinPoint){		String methodName = joinPoint.getSignature().getName();		List args = Arrays.asList(joinPoint.getArgs());		System.out.println("The method " + methodName + " begins with "+ args);	}		//目标方法正常结束后执行	@AfterReturning(value="declareJointPointExpression()"			,returning="result")	public void afterReturning(JoinPoint joinPoint,Object result){		String methodName = joinPoint.getSignature().getName();		System.out.println("The method " + methodName + " ends with " + result);	}		//目标方法出现异常时执行	//可以访问到异常对象,可在出现特定异常时执行	@AfterThrowing(value="declareJointPointExpression()"			,throwing="exception")	public void aferThrowing(JoinPoint joinPoint,Exception exception){		String methodName = joinPoint.getSignature().getName();		System.out.println("The method " + methodName + " occurs " + exception);	}		//声明为后置通知:在目标方法执行之后(无论是否发生异常)执行	//后置通知中不能访问到目标方法执行的结果	@After("declareJointPointExpression()")	public void afterMethod(JoinPoint joinPoint){		String methodName = joinPoint.getSignature().getName();		System.out.println("The method " + methodName + " ends ");	}		//环绕通知要携带ProceedingJoinPoint类型的参数	//环绕通知类似于动态代理的全过程,可实现其它通知	//环绕通知必须有返回值,即目标方法的返回值	@Around("declareJointPointExpression()")	public Object arouncMethod(ProceedingJoinPoint pjd){		Object result = null;		String methodName = pjd.getSignature().getName();		//执行目标方法		try {			//前置通知			System.out.println("The method " + methodName + " begins with "+ Arrays.asList(pjd.getArgs()));			result = pjd.proceed();			//返回通知			System.out.println("The method " + methodName + " ends with " + result);		} catch (Throwable e) {			//异常通知			System.out.println("The method " + methodName + " occurs " + e);			throw new RuntimeException(e);		}		//后置通知		System.out.println("The method " + methodName + " ends ");		return result;	}}

创建Main.java类进行测试
package cn.baokx.spring.aop.helloworld;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {	public static void main(String[] args) {		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");		HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");		int result = helloWorld.divide(10, 2);		result = helloWorld.divide(10, 0);	}}

转载于:https://my.oschina.net/u/1427708/blog/710611

你可能感兴趣的文章