pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.16.RELEASE</version> </dependency>
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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--启用注解--> <context:component-scan base-package="com.baccarat.aop" /> <!-- 启用 aspectj 方式 AOP--> <aop:aspectj-autoproxy proxy-target-class="true" /> </beans>
Advices.java
注意切入点 所在包下要对不然启动不成功
@Pointcut("execution(* com.baccarat.service.impl.UserServiceImpl.*(..))")
package com.baccarat.aop; import java.lang.reflect.Method; import java.util.Date; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; 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.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.baccarat.entity.User; import com.baccarat.util.BaccaratUtil; @Component @Aspect public class Advices { final static Logger logger = Logger.getLogger(Advices.class); @Pointcut("execution(* com.baccarat.service.impl.UserServiceImpl.*(..))") public void controllerAspect() { } /** * before * * @author Stephen * @Time 2019年9月24日 15:51:03 */ @Before("controllerAspect()") public void before(JoinPoint jp) { // getClass String className = jp.getTarget().getClass().getName(); // getMethod String methodName = jp.getSignature().getName(); String str = className + "---" + methodName; Object[] params = jp.getArgs(); for (Object param : params) { logger.info("@Before:params:" + param); } logger.info(str); MethodSignature methodSignature = (MethodSignature) jp.getSignature(); // getMethod Method method = methodSignature.getMethod(); // get note ILog logAnno = method.getAnnotation(ILog.class); // params String operateType = logAnno.operationType(); String operateName = logAnno.operationName(); logger.info("type:" + operateType); logger.info("action:" + operateName); } /** * after inform * * @author Stephen * @Time 2019年9月25日 14:48:49 */ @After("controllerAspect()") public void after(JoinPoint joinPoint) { logger.info("success"); logger.info("over..."); } }
Ilog.java
自定义注解
package com.baccarat.aop; import java.lang.annotation.ElementType; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.RetentionPolicy; @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ILog { /** 要执行的操作类型比如:add操作 **/ public String operationType() default ""; /** 要执行的具体操作比如:添加用户 **/ public String operationName() default ""; }
service
自定义注解
结果