第一步导入依赖
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.3</version> </dependency>
第二步写一个切面类
package com.Aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * @author xulei * @version 1.0 * @date 2020/3/29 22:12 */ @Aspect public class FirstAspect { @Before("execution(* com.Dao.*.*(..))") public void before(JoinPoint joinPoint){ System.out.println("切面before方法"); } }
第三步写一个配置类(不写配置类无法切面无法生效)
package com.config; import com.Aspect.FirstAspect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * @author xulei * @version 1.0 * @date 2020/3/29 22:21 */ @EnableAspectJAutoProxy @Configuration public class AspectConfig { @Bean public FirstAspect controllerAspect(){ return new FirstAspect(); } }