AOP切面编程三
博客专区 > scooplol 的博客 > 博客详情
AOP切面编程三
scooplol 发表于5个月前
AOP切面编程三
  • 发表于 5个月前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 基于Annotation配置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); 
     }
    
}

 

共有 人打赏支持
粉丝 2
博文 36
码字总数 68238
×
scooplol
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: