AOP 的第一次使用
maven中添加的依赖
<!-- Spring AOP --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency>
Spring中的配置
1.beans 头文件的配置
xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
2.开启标注形式的AOP
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
创建一个类使用类上加入@Component @Aspect 标注
package cn.lzy.aspect; import cn.lzy.util.CookieUtil; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * @ClassName WxAspect * @Author:Liziy * @Date 2020/8/13 19:06 * @Description: **/ @Component @Aspect public class WxAspect {
/** @Around 环绕通知 * */ @Around("execution(public String cn.lzy.controller.WXController.wxAdmin*())") public void wxAdminAspect() {
System.out.println("进入wxAdminAspect()---");
}
}
会匹配WXController类下以wxAdmin..名字为所有方法,方法执行前后都会匹配