第一步:修改applicationContext文件java
<?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配置spring
1.業務層apache
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.處理類spring-mvc
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); } }