1. 基于XML的AOP开发
1.1 快速入门
步骤分析:
- 创建java项目,导入AOP相关坐标
- 创建目标接口和目标实现类(定义切入点)
- 创建通知类及方法(定义通知)
- 将目标类和通知类对象创建权交给spring
- 在核心配置文件中配置织入关系,及切面
- 编写测试代码
1.2 创建java项目,导入AOP相关坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lagou</groupId>
<artifactId>spring_aop_xml</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--导入spring的context坐标,context依赖aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- aspectj的织入(切点表达式需要用到该jar包) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
1.3 创建目标接口和目标实现类
package com.lagou.servlet;
public interface AccountService {
/*
目标方法:(切入点:要进行拦截增强的方法)
*/
public void transfer();
}
package com.lagou.servlet.impl;
import com.lagou.servlet.AccountService;
public class AccountServiceImpl implements AccountService {
/*
目标方法:(切入点:要进行拦截增强的方法)
*/
public void transfer() {
System.out.println("转账方法执行了....");
//int i = 1/0;
}
}
1.4 创建通知类
package com.lagou.advice;
import org.aspectj.lang.ProceedingJoinPoint;
/*
通知类
*/
public class MyAdvice {
public void before(){
System.out.println("前置通知执行了....");
}
public void afterReturning(){
System.out.println("后置通知执行了....");
}
public void afterThrowing(){
System.out.println("异常通知执行了....");
}
public void after(){
System.out.println("最终通知执行了....");
}
// Proceeding JoinPoint : 正在执行的连接点:切点
public Object around(ProceedingJoinPoint pjp){
// 切点方法执行
Object proceed = null;
try {
System.out.println("前置通知执行了");
proceed = pjp.proceed();
System.out.println("后置通知执行了");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("异常通知执行了");
}finally {
System.out.println("最终通知执行了");
}
return proceed;
}
}
1.5 将目标类和通知类对象创建权交给spring
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--目标类交给IOC容器-->
<bean id="accountServcie" class="com.lagou.servlet.impl.AccountServiceImpl"></bean>
<!--通知类交给IOC容器-->
<bean id="myAdvice" class="com.lagou.advice.MyAdvice"></bean>
</beans>
1.6 在核心配置文件中配置织入关系,及切面
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--目标类交给IOC容器-->
<bean id="accountServcie" class="com.lagou.servlet.impl.AccountServiceImpl"></bean>
<!--通知类交给IOC容器-->
<bean id="myAdvice" class="com.lagou.advice.MyAdvice"></bean>
<!--
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
execution(public void com.lagou.servlet.impl.AccountServiceImpl.transfer(java.lang.String))
- 访问修饰符可以省略
execution(void com.lagou.servlet.impl.AccountServiceImpl.transfer(java.lang.String))
- 返回值类型、包名、类名、方法名可以使用星号 * 代替,代表任意
execution(* *.*.*.*.*.*())
- 包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
execution(* *..*.*())
- 参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
execution(* *..*.*(..))
-->
<!--AOP配置-->
<aop:config>
<!--抽取的切点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* com.lagou.servlet.impl.AccountServiceImpl.*(..))"/>
<!--配置切面:切入点+通知-->
<aop:aspect ref="myAdvice">
<!-- <aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
<aop:after method="after" pointcut-ref="myPointcut"/>-->
<aop:around method="around" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
1.7 编写测试代码
package com.lagou.test;
import com.lagou.servlet.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class AccountServiceImplTest {
@Autowired
private AccountService accountService;
@Test
public void testTransfer(){
accountService.transfer();
}
}
1.8 XML配置AOP详解
1.8.1 切点表达式
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
- 访问修饰符可以省略
- 返回值类型、包名、类名、方法名可以使用星号 * 代替,代表任意
- 包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
- 参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
例如:
execution(public void com.lagou.service.impl.AccountServiceImpl.transfer())
execution(void com.lagou.service.impl.AccountServiceImpl.*(..))
execution(* com.lagou.service.impl.*.*(..))
execution(* com.lagou.service..*.*(..))
切点表达式抽取:
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替pointcut 属性来引用抽取后的切点表达式.
<!--AOP配置-->
<aop:config>
<!--抽取的切点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* com.lagou.servlet.impl.AccountServiceImpl.*(..))"/>
<!--配置切面:切入点+通知-->
<aop:aspect ref="myAdvice">
<!-- <aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
<aop:after method="after" pointcut-ref="myPointcut"/>-->
<aop:around method="around" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
1.8.2 通知类型
通知的配置语法:
<aop:通知类型 method=“通知类中方法名” pointcut=“切点表达式"></aop:通知类型>
名称 | 标签 | 说明 |
前置通知 |
<aop:before> | 用于配置前置通知。指定增强的方法在切入点方法之前执行 |
后置通知 |
<aop:afterReturning> | 用于配置后置通知。指定增强的方法在切入点方法之后执行 |
异常通知 |
<aop:afterThrowing> | 用于配置异常通知。指定增强的方法出现异常后执行 |
最终通知 |
<aop:after> | 用于配置最终通知。无论切入点方法执行时是否有异常, 都会执行 |
环绕通知 |
<aop:around> | 用于配置环绕通知。开发者可以手动控制增强代码在什么时候执行 |
注意:通常情况下,环绕通知都是独立使用的
2. 知识小结
* aop织入的配置
<aop:config>
<aop:aspect ref=“通知类”>
<aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
</aop:aspect>
</aop:config>
- 通知的类型
前置通知、后置通知、异常通知、最终通知环绕通知
- 切点表达式
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
节选自拉钩教育JAVA系列课程