Java通过Spring、Aspectj实现AOP(基于XML)

首先,导入包:

blob.png


首先,AOP联盟定义的类型分为:

前置通知 org.springframework.aop.MethodBeforeAdvice,在目标方法执行前实施增强(执行切面方法)

后置通知 org.springframework.aop.AfterReturningAdvice,在目标方法执行后实施增强(执行切面方法)

环绕通知 org.aopalliance.intercept.MethodInterceptor,在目标方法执行前后实施增强(执行切面方法)

异常抛出通知 org.springframework.aop.ThrowsAdvice,在方法抛出异常后实施增强(执行切面方法)

引介通知 org.springframework.aop.IntroductionInterceptor,在目标类中添加一些新的方法和属性

其中,环绕通知用的最多。


假设已有如下目标接口和目标类:

public interface UserService {
	void addUser();

	String updateUser();

	void deleteUser();
}
public class UserServiceImpl implements UserService {

	@Override
	public void addUser() {
		System.out.println(" add user");
	}

	@Override
	public String updateUser() {
		//int a = 1/0;
		System.out.println(" update user");
		return "更新用户信息";
	}

	@Override
	public void deleteUser() {
		System.out.println(" delete user");
	}

}

在此基础上,不修改源代码,在调用UserServiceImpl的实例方法前后,输出其他的信息。

新建一个切面类:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;


public class MyAspect {

	//自定义前置通知方法
	public void myBefor(JoinPoint joinPoint) {
		System.out.println("前置通知");
	}
	
	/**
	 * 自定义后置通知方法
	 * @param joinPoint
	 * @param object 目标方法的返回值
	 */
	public void myReturn(JoinPoint joinPoint, Object object) {
		System.out.println("后置通知,返回的值为:" + object);
	}
	//自定义环绕通知方法
	public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕前 通知");
		Object object = joinPoint.proceed();
		System.out.println("环绕后通知");
		return object;
	}
	/**
	 * 自定义异常抛出通知方法
	 * @param joinPoint
	 * @param eThrowable 目标方法的异常
	 */
	public void myThrowAble(JoinPoint joinPoint, Throwable eThrowable) {
		System.out.println("异常通知:" + eThrowable.getMessage());
	}
}

然后添加配置文件:

<?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="userService" class="com.kye.d_aspect.a_xml.UserServiceImpl"></bean>

	<bean id="myAspect" class="com.kye.d_aspect.a_xml.MyAspect"></bean>

	<aop:config>
		<aop:aspect ref="myAspect">
			<aop:pointcut expression="execution(* com.kye.d_aspect.a_xml.UserServiceImpl.*(..))" id="myCut" />
			 <aop:before method="myBefor"  pointcut-ref="myCut" /> 
			 
			 <aop:after-returning method="myReturn" pointcut-ref="myCut" returning="object"/>
			
		  	<aop:around method="myAround"   pointcut="execution(* com.kye.d_aspect.a_xml.UserServiceImpl.update*(..))"/> 
		  	
		  	
		  	<aop:after-throwing method="myThrowAble" pointcut-ref="myCut" throwing="eThrowable"/>
		</aop:aspect>
	</aop:config>

</beans>

切入点表达式可以参考这里

然后测试类:

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

public class MyTest {

	@Test
	public void test1() {

		String path = "com/kye/d_aspect/a_xml/beans.xml";

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
		UserService userService = applicationContext.getBean("userService", UserService.class);
 
		userService.addUser();
		userService.updateUser();
		userService.deleteUser();
	}

}

最后输出:

befor...
c_spring_aop add user
after...
befor...
c_spring_aop update user
after...
befor...
c_spring_aop delete user
after...


原文地址:https://www.cnblogs.com/wugang/p/14232332.html