spring aop|xml配置
关于aop的一些基础概念可以看 spring aop|一些相关概念
需要配置的标签介绍
aop:config标签
-
作用:用于表示开始aop的配置
-
出现位置:写在beans标签的内部
-
属性:
- proxy-target-class:用于指定代理方式。默认值是false。当取值为true时,采用cglib的代理方式。
- expose-proxy:用于指定是否暴露代理对象,通过AopContext可以进行访问代理对象。
aop:aspect标签
Aspect概念:一个模块具有一组提供横切需求的 APIs。
例如,一个日志模块为了记录日志将被 AOP 方面调用。应用程序可以拥有任意数量的方面,这取决于需求。
简单的说就是用于被调用的方法前后增强的那些方法对应的那个类
- 作用: 用于配置切面。
- 出现位置: aop:config标签内部。
- 属性:
- id:用于指定切面的唯一标识。
- ref:用于指定引用bean的id。 o
- rder:用于指定多个切面中,相同通知类型的执行顺序。取值是个整数,数值越小优先级越高
aop:pointcut标签
简单的说这个是对被代理的函数的一个配置,也就是给谁添加上那些前后环绕的增强的代码,pointcut表达式就是通过一种表达式的方式去匹配符合表达式的函数
pointcut表达式的编写详见 spring aop|切入点pointcut表达式编写方案示例
- 作用: 用于配置通用切入点表达式
- 出现位置:
- aop:config标签内部,当出现在此处时,要求必须在所有aop:aspect标签之前。它可以供所有切面使用
- aop:aspect标签内部,当出现在此处时,它没有顺序要求,但只能供当前切面使用。
五种通知类型的标签
它们只能出现在aop:aspect标签内部
aop:before
- 作用: 用于配置前置通知。
- 属性:
- method:用于指定前置通知的方法名称
- pointcut:用于指定切入点表达式
- pointcut-ref:用于指定切入点表达式的引用
aop:after-returning
- 作用: 用于配置后置通知 ,在被增强的函数正常的return之后执行
- 属性:
- method:用于指定后置通知的方法名称
- pointcut:用于指定切入点表达式
- pointcut-ref:用于指定切入点表达式的引用
- returning:用于指定返回值类型
aop:after-throwing 作用:
- 作用:用于配置异常通知。
- 属性:
- method:用于指定异常通知的方法名称
- pointcut:用于指定切入点表达式
- pointcut-ref:用于指定切入点表达式的引用
- arg-names:用于指定通知方法的参数名称,要求表达式中必须有描述args的语句
- throwing:用于指定异常通知中异常的变量名称
aop:after
- 作用:用于指定最终通知。
- 属性:
- method:用于指定最终通知的方法名称
- pointcut:用于指定切入点表达式
- pointcut-ref:用于指定切入点表达式的引用
- arg-names:用于指定通知方法的参数名称,要求表达式中必须有描述args的语句
aop:around
环绕通知,它是有别于前面四种通知类型外的特殊通知。前面四种通知(前置,后置,异常和最终)它们都是指定何时增强的通知类型。而环绕通知,它是Spring框架为我们提供的一种可以通过编码的方式,控制增强代码何时执行的通知类型。它里面借助的ProceedingJoinPoint接口及其实现类,实现手动触发切入点方法的调用。
- 作用:用于配置环绕通知
- 属性:
- method:用于指定环绕通知的方法名称
- pointcut:用于指定切入点表达式
- pointcut-ref:用于指定切入点表达式的引用
- arg-names:用于指定环绕通知方法的参数名称,要求表达式中必须有描述args的语句
eg
beans配置文件
<?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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="aspect" class="com.kehao.aspect.MyLogAspect"></bean>
<bean id="student" class="com.kehao.pojo.Student"></bean>
<aop:config>
<aop:aspect id="log" ref="aspect">
<aop:pointcut id="speak" expression="execution(* com.kehao.pojo.Student.speak(..))"/>
<aop:before method="logBefore" pointcut-ref="speak"/>
<aop:after-returning method="logAfterReturn" pointcut-ref="speak"/>
<aop:after-throwing method="logAfterThrowException" pointcut-ref="speak"/>
<aop:after method="logAfter" pointcut-ref="speak"/>
</aop:aspect>
</aop:config>
</beans>
MyLogAspect.java
public class MyLogAspect {
public void logBefore(JoinPoint joinPoint) throws Throwable {
System.out.println("[logBefore]");
}
public void logAfterReturn(JoinPoint joinPoint) throws Throwable {
System.out.println("[logAfterReturn]");
}
public void logAfterThrowException(JoinPoint joinPoint) throws Throwable {
System.out.println("[logAfterThrowException]");
}
public void logAfter(JoinPoint joinPoint) throws Throwable {
System.out.println("[logAfter]");
}
}
Student.java
public class Student {
public void speak(){
System.out.println("i love Java!");
}
}
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class StudentTest {
@Autowired
private Student student;
@Test
public void speak() {
student.speak();
}
}
具体代码详见:https://github.com/chenkehao1998/JavaExampleForBlog/tree/main/spring_aop_xml