spring 13-Spring框架基于Annotation的AOP配置

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

package cn.mldn.service;
public interface IMessageService {
	public boolean remove(String mid);
}
package cn.mldn.service.impl;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import cn.mldn.service.IMessageService;
@Service
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 java.util.Arrays;
import org.apache.log4j.Logger;
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;
@Aspect
@Component
public class ServiceProxy {
	@Around("execution(* cn.liang.service..*.*(..))")
	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 ;
	} 
	@Before(value="execution(* cn.liang.service..*.*(..)) and args(id)",argNames="id")
	public void beforeInvoke(Object arg) {
		Logger.getLogger(ServiceProxy.class).info("【ServiceProxy - BEFORE】在业务方法执行之前进行调用,参数内容:" + arg);
	} 
	@AfterThrowing(value="execution(* cn.liang.service..*.*(..))",throwing="e",argNames="e")
	public void throwInvoke(Exception e) {
		Logger.getLogger(ServiceProxy.class).error("【ServiceProxy - EXCEPTION】抛出异常:" + e);
	}
	@After("execution(* cn.liang.service..*.*(..))")
	public void afterInvoke() {
		Logger.getLogger(ServiceProxy.class).info("【ServiceProxy - AFTER】在业务方法执行之后进行调用。");
	}
	@AfterReturning(value="execution(* cn.liang.service..*.*(..))",returning="n",argNames="n")
	public void returnInvoke(Object val) {
		Logger.getLogger(ServiceProxy.class).info("【ServiceProxy - RETURNING】返回值 = " + val);
	}
}

3、配置applicationContext.xml文件

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	<aop:aspectj-autoproxy/>
	<context:annotation-config/>
	<context:component-scan base-package="cn.liang"/>
</beans>

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 17:09:26,791 INFO [cn.liang.service.proxy.ServiceProxy] - 【*** BEFORE ***】执行参数:[101]
2018-12-05 17:09:26,795 INFO [cn.liang.service.proxy.ServiceProxy] - 【ServiceProxy - BEFORE】在业务方法执行之前进行调用,参数内容:1234
2018-12-05 17:09:26,796 INFO [cn.liang.service.IMessageService] - 【业务层】执行删除调用,删除的数据ID = 1234
2018-12-05 17:09:26,796 INFO [cn.liang.service.proxy.ServiceProxy] - 【*** AFTER ***】返回结果:false
2018-12-05 17:09:26,796 INFO [cn.liang.service.proxy.ServiceProxy] - 【ServiceProxy - AFTER】在业务方法执行之后进行调用。
2018-12-05 17:09:26,796 INFO [cn.liang.service.proxy.ServiceProxy] - 【ServiceProxy - RETURNING】返回值 = true
true
原文地址:https://www.cnblogs.com/liangjingfu/p/10072245.html