一、spring aop execution表達式說明
在使用spring框架配置AOP的時候,不管是通過XML配置文件還是注解的方式都需要定義pointcut"切入點"
例如定義切入點表達式 execution(* com.sample.service.impl..*.*(..))
execution()是最常用的切點函數,其語法如下所示:
整個表達式可以分為五個部分:
1、execution(): 表達式主體。
2、第一個*號:表示返回類型,*號表示所有的類型。
3、包名:表示需要攔截的包名,後面的兩個句點表示當前包和當前包的所有子包,com.sample.service.impl包、子孫包下所有類的方法。
4、第二個*號:表示類名,*號表示所有的類。
5、*(..):最後這個星號表示方法名,*號表示所有的方法,後面括弧裡面表示方法的參數,兩個句點表示任何參數。
二、程序
1.要記錄日志的某個方法: updatePromote
/** * 修改商品活動 * @param vo * @param promoteId為,AOP監控的查詢ID * @return * @throws Exception */ public ResultVO updatePromote(PromoteVO vo,Long promoteId)throws Exception;
2.增加一個橫切關注點,打印日志,Java類為
package com.fortis.drugstore.web.userdbThrift.aop; import java.util.Date; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.fortis.drugstore.base.action.BaseAction; import com.fortis.drugstore.system.utils.SessionInfo; import com.fortis.drugstore.web.userdbThrift.activity.service.PromoteService; import com.fortis.thrift.userdb.PromoteVO; //聲明這是一個組件 @Component //聲明這是一個切面Bean @Aspect public class ServiceAspect extends BaseAction<Object>{ private static final long serialVersionUID = 7690224540336380592L; private final static Logger log = Logger.getLogger(ServiceAspect.class); @Autowired private PromoteService service; //配置切入點,該方法無方法體,主要為方便同類中其他方法使用此處配置的切入點 @Pointcut("execution(* com.fortis.drugstore.web.userdbThrift.activity.service.impl..PromoteServiceImpl.updatePromote(..))") public void aspect(){ } /* * 配置前置通知,使用在方法aspect()上注冊的切入點 * 同時接受JoinPoint切入點對象,可以沒有該參數 */ @Before("aspect()") public void before(JoinPoint joinPoint){ PromoteVO obBefore = new PromoteVO(); Object []param = joinPoint.getArgs(); if(log.isInfoEnabled()){ Object promoteId = param[1]; //切點中有兩個參數,第二個為表的主鍵,用於查詢 SessionInfo sessionInfo = (SessionInfo) getSession().getAttribute("sessionInfo"); try { obBefore = service.queryPromoteDetail(Long.valueOf(promoteId.toString())); } catch (Exception e) { e.printStackTrace(); } log.info("【修改活動數據】:"+new Date()+" 【賬號】:"+sessionInfo.getUserAcct()+" 【名稱】:"+sessionInfo.getUserName()); log.info("修改之前:"+obBefore.toString()); } } //配置後置通知,使用在方法aspect()上注冊的切入點 @After("aspect()") public void after(JoinPoint joinPoint){ PromoteVO obAfter = new PromoteVO(); Object []param = joinPoint.getArgs(); if(log.isInfoEnabled()){ Object promoteId = param[1]; try { obAfter = service.queryPromoteDetail(Long.valueOf(promoteId.toString())); } catch (Exception e) { e.printStackTrace(); } log.info("修改之後:"+obAfter.toString()); } } }
3.spring aop的配置文件 spring-aop.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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 激活自動代理功能 --> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>