多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
AspectJ 是一个独立的 AOP 框架,Spring 整合了该框架来实现注解式的 AOP 编程。 ``` ====常用的 AOP 注解==== @Order(int value) 决定切面对象起作用的顺序,value越小越先被执行 @Aspect 将切面对象注入IoC容器中 @Pointcut 定义切入点 @Before 前置增强 @After 最终增强 @AfterReturning 后置增强 @AfterThrowing 异常增强 @Around 环绕增强 ``` <br/> 开启 AOP 注解开发步骤如下: **1. maven坐标** ```xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.9</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> ``` **2. 开启包扫描与AspectJ代理`resources/ApplicationContext.xml`** ```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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://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="learn.springaopanno" /> <!-- 开启AspectJ代理 --> <aop:aspectj-autoproxy /> </beans> ``` **3. 目标对象** ```java @Service("userService2") public class UserServiceImpl implements UserService { @Override public String getUsername() { System.out.println("------目标函数的输出------\n[getUsername]"); return "[getUsername]"; } } ``` **4. 切面对象** ```java @Aspect @Order(125) //该注解可选 @Component public class AspectLogger { @Before("execution(public String getUsername())") public void printLog(JoinPoint jPoint) { System.out.println("-----增强函数的输出------" + "\n增强方式:前置增强" + "\n目标对象:" + jPoint.getTarget() + "\n目标函数:" + jPoint.getSignature().getName() + "\n目标函数的参数:" + Arrays.toString(jPoint.getArgs())); } } ``` **5. 测试** ```java public class UserTest { private final ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); @Test public void testGetUsername() { //添加切面后,不能是实现类,如context.getBean(UserServiceImpl.class); UserService userService = context.getBean("userService2", UserService.class); userService.getUsername(); } } ``` ``` /********** 结果如下 **********/ -----增强函数的输出------ 增强方式:前置增强 目标对象:learn.springaopanno.service.impl.UserServiceImpl@5ef5c734 目标函数:getUsername 目标函数的参数:[] ------目标函数的输出------ [getUsername] ``` <br/> 注解式的切入点写法如下: ```java //写法1 @Before("execution(public String getUsername())") public void printLogger(JoinPoint jPoint) {...} //写法2:先封装切入点,再应用到增强函数中 @Pointcut(value = "execution(public String getUsername())") public void getUsernamePointcut() {/* 封装的切入点*/} @Before("getUsernamePointcut()") public void printLogger(JoinPoint jPoint) {...} //写法3:可以同时配置多个切入点,使用&&或||来隔开 @Pointcut("(execution(public String getUsername())) && (execution(public String getUsername2()))") public void getUsernamePointcut() { /*封装的切入点 */} @Before("getUsernamePointcut()") public void printLogger(JoinPoint jPoint) {...} ``` **** 案例源码:https://gitee.com/flymini/codes03/tree/master/learn-springaopanno