AOP切面编程三 原

第一步:修改applicationContext文件

<?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:task="http://www.springframework.org/schema/task"  
	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.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task-4.1.xsd ">
     <aop:aspectj-autoproxy />
     <context:annotation-config/>
     <context:component-scan base-package="group.esperanto"/>
</beans>

第二步:业务层和处理类添加Annotation配置

1.业务层

package group.esperanto.service.impl;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import group.esperanto.service.IMessageService;
@Service
public class MessageServiceImpl implements IMessageService {
	@Override
	public boolean doRemove(String mid) {
		Logger.getLogger(IMessageService.class).info("[业务层]---执行删除ID = "+mid);
		return false;
	}
}

2.处理类

package group.esperanto.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.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(* group.esperanto.service..*.*(..))")
     public Object arroundInvoke(ProceedingJoinPoint point) throws Throwable{
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy -arroundInvoke Before] 参数 值 "+ Arrays.toString(point.getArgs())); 
           // Object obj = point.proceed(point.getArgs());  // 正常继续传递参数
    	 Object obj = point.proceed(new Object[]{"hello"});   //现在不传递参数,自定义参数传递
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy -arroundInvoke After] 返回结果"+obj); 
    	 return true;
     }
	 @Before(value="execution(* group.esperanto.service..*.*(..)) and args(id)",argNames="id")
     public void beforeInvoke(Object arg){
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Before] 参数值 "+arg); 
     }
	 @After("execution(* group.esperanto.service..*.*(..))")
     public void afterInvoke(){
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - After]"); 
     }
	 @AfterReturning(value="execution(* group.esperanto.service..*.*(..))",returning="val",argNames="val")
     public void returnInvoke(Object val){
    	 Logger.getLogger(ServiceProxy.class).info("[ServiceProxy - Returning] 返回值 "+val); 
     }
    
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区

领取腾讯云代金券