AOP
准备
spring-boot项目
- 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
war项目
- 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.3</version>
</dependency>
- application.xml
<beans xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启aop-->
<aop:aspectj-autoproxy />
</beans>
使用
- 通过注解
@Aspect
public class MyAop{
@Pointcut("execution(* com.wl.sb.controller.FirstController.*(..))")
public void myPointCut(){}
@Around("myPointCut()")
public Object handlerControllerMethod(ProceedingJoinPoint pjp){
//执行当前切入点方法
pjp.proceed();
//获取当前切入点带包路径
pjp.getSignature();
}
}
- 通过配置的方式
<bean id="controllerAop" class="com.wl.aop.ControllerAop" />
<aop:config>
<aop:aspect id="myAop" ref="controllerAop">
<aop:pointcut id="target"
expression="execution(* com.wl.controller.FirstController.*(..))" />
<aop:around method="handlerControllerMethod" pointcut-ref="target" />
</aop:aspect>
</aop:config>