spring.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 扫描注解bean --> <context:component-scan base-package="cn.us.aspect"/> <!-- 开启切面代理 使得spring认识 @Aspect --> <aop:aspectj-autoproxy/>
java文件中 无参数的
package cn.us.aspect; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import cn.us.domain.User; @Component("logAspect") @Aspect public class LogAspect { // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) and args(user)") 以下是无参数的写法 @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(..))") public void pointcut() {} // @Before("pointcut(user)") @Before("pointcut()") // public void startRecordLog(User user) public void startRecordLog() { System.out.println("Before log has started"); System.out.println(); } @AfterReturning(value="pointcut()",returning="val") public void endRecordLog(Object val) { System.out.println("@AfterReturning log has end"); System.out.println(val); } }
有参数的 利用 @[email protected]
有参数 传递的写法 @Before和 @After 的形参都要写上 参数
package cn.us.aspect; import org.aspectj.lang.ProceedingJoinPoint; 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.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import cn.us.domain.User; @Component("logAspect") @Aspect public class LogAspect { // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)") @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(cn.us.domain.User)) && args(user) ") public void pointcut(User user) {} // @Before(value="pointcut(user)") // public void startRecordLog(cn.us.domain.User user) // @Before("pointcut()") // public void startRecordLog() @Before(value="pointcut(user)") public void startRecordLog(User user) { System.out.println("Before log has started"); System.out.println(user); } @AfterReturning(value="pointcut(user)",returning="val") public void endRecordLog(User user,Object val) { User u=(User) val; //这里要强制类型转换,否则输出的位null ,没有强转调用object的,所以就为null了 System.out.println("@AfterReturning log has end"); System.out.println(u); } @Around(value = "pointcut(user)") public void aroundRecordLog(ProceedingJoinPoint pjp,User user) throws Throwable { System.out.println("around before"); //获取参数 Object [] objs=pjp.getArgs(); for(Object obj :objs) { System.out.println(obj); } //返回值 Object oo=pjp.proceed(); System.out.println("around after" +oo); } }
无参数的写法,利用环绕通知
可以再环绕通知里 利用getArgs()方法获取参数。并且输出
package cn.us.aspect; import org.aspectj.lang.ProceedingJoinPoint; 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.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import cn.us.domain.User; @Component("logAspect") @Aspect public class LogAspect { // @Pointcut("execution(* cn.us.service.impl.UserServiceImpl.*(cn.us.domain.User)) and args(user)") @Pointcut(value="execution(* cn.us.service.impl.UserServiceImpl.addUser(..)) ") public void pointcut() {} @Around(value = "pointcut()") public void aroundRecordLog(ProceedingJoinPoint pjp) throws Throwable { System.out.println("around before"); //获取参数 Object [] objs=pjp.getArgs(); for(Object obj :objs) { System.out.println(obj); } //返回值 Object oo=pjp.proceed(); System.out.println("around after" +oo); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17204.html