spring 12-Spring框架AOP深入配置

AOP深入配置可以实现以下功能:

  • 前置通知的参数传递
  • 返回通知
  • 针对异常进行处理

1、定义业务层接口和子类

package cn.liang.service;
public interface IMessageService {
	public boolean remove(String mid) ;
}
package cn.liang.service.impl;
import org.apache.log4j.Logger;
import cn.liang.service.IMessageService;
public class MessageServiceImpl implements IMessageService {
	@Override
	public boolean remove(String mid) {
		Logger.getLogger(IMessageService.class).info("【业务层】执行删除调用,删除的数据ID = " + mid);
		return false;
	}
}

2、定义一个描述AOP程序处理的结构类

package cn.liang.service.proxy;
import org.apache.log4j.Logger;
public class ServiceProxy {
	public void beforeInvoke(Object arg) {
		Logger.getLogger(ServiceProxy.class).info("【ServiceProxy - BEFORE】在业务方法执行之前进行调用,参数内容:" + arg);
	} 
	public void throwInvoke(Exception e) {
		Logger.getLogger(ServiceProxy.class).error("【ServiceProxy - EXCEPTION】抛出异常:" + e);
	}
	public void afterInvoke() {
		Logger.getLogger(ServiceProxy.class).info("【ServiceProxy - AFTER】在业务方法执行之后进行调用。");
	}
	public void returnInvoke(Object val) {
		Logger.getLogger(ServiceProxy.class).info("【ServiceProxy - RETURNING】返回值 = " + val);
	}	
}

3、配置applicationContext.xml文件

<bean id="messageServiceImpl" class="cn.liang.service.impl.MessageServiceImpl"/>
<bean id="serviceProxy" class="cn.liang.service.proxy.ServiceProxy"/>
<aop:config>
	<aop:pointcut expression="execution(* cn.liang.service..*.*(..)) and args(id)"
		id="defaultPointCut" />
	<aop:aspect ref="serviceProxy">
		<aop:before method="beforeInvoke" pointcut-ref="defaultPointCut"
			arg-names="id" />
		<aop:after method="afterInvoke" pointcut="execution(* cn.liang.service..*.*(..))" />
		<aop:after-returning method="returnInvoke"
			pointcut="execution(* cn.liang.service..*.*(..))" returning="v"
			arg-names="v" />
		<aop:after-throwing method="throwInvoke"
			pointcut="execution(* cn.liang.service..*.*(..))" throwing="e"
			arg-names="e" />
	</aop:aspect>
</aop:config>

4、编写测试程序

package cn.liang.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.liang.service.IMessageService;
public class TestAOP {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		IMessageService msgService = ctx.getBean("messageServiceImpl", IMessageService.class);
		System.out.println(msgService.remove("101"));
	}
}

5、输出结果

2018-12-05 16:54:01,793 INFO [cn.liang.service.proxy.ServiceProxy] - 【ServiceProxy - BEFORE】在业务方法执行之前进行调用,参数内容:101
2018-12-05 16:54:01,794 INFO [cn.liang.service.IMessageService] - 【业务层】执行删除调用,删除的数据ID = 101
2018-12-05 16:54:01,794 INFO [cn.liang.service.proxy.ServiceProxy] - 【ServiceProxy - AFTER】在业务方法执行之后进行调用。
2018-12-05 16:54:01,794 INFO [cn.liang.service.proxy.ServiceProxy] - 【ServiceProxy - RETURNING】返回值 = false
false

AOP深入配置之环绕通知

1、定义一个描述AOP程序处理的环绕通知结构类

package cn.liang.service.proxy;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
public class ServiceProxy {
	// 如果要进行后续调用需要知道传递的参数,需要知道具体要调用的业务方法
		public Object arroundInvoke(ProceedingJoinPoint point) throws Throwable {
			Logger.getLogger(ServiceProxy.class).info("【*** BEFORE ***】执行参数:" + Arrays.toString(point.getArgs())); 
			// Object obj = point.proceed(point.getArgs()) ;正常操作要将用户的参数继续向后传递
			Object obj = point.proceed(new Object[] {"1234"} ) ;	// 自己来处理参数内容
			Logger.getLogger(ServiceProxy.class).info("【*** AFTER ***】返回结果:" + obj);
			return true ;
		}	
}

2、配置applicationContext.xml文件

<bean id="messageServiceImpl" class="cn.liang.service.impl.MessageServiceImpl"/>
<bean id="serviceProxy" class="cn.liang.service.proxy.ServiceProxy"/>
 <aop:config>
 	<aop:pointcut expression="execution(* cn.liang.service..*.*(..))" id="defaultPointCut" />
	<aop:aspect ref="serviceProxy">
		<aop:around method="arroundInvoke" pointcut-ref="defaultPointCut"/>
	</aop:aspect>
 </aop:config>

3、使用TestAOP测试结果

2018-12-05 17:01:18,190 INFO [cn.liang.service.proxy.ServiceProxy] - 【*** BEFORE ***】执行参数:[101]
2018-12-05 17:01:18,190 INFO [cn.liang.service.IMessageService] - 【业务层】执行删除调用,删除的数据ID = 1234
2018-12-05 17:01:18,190 INFO [cn.liang.service.proxy.ServiceProxy] - 【*** AFTER ***】返回结果:false
true
原文地址:https://www.cnblogs.com/liangjingfu/p/10072191.html