基于 Schema的AOP是 Spring20发布之后新增加的一种AOP使用方式,我们可以从如下两个角度来看待基于 Schema的AOP:
-
配置配置方式的改变。
Spring框架从1x版本升级到2x版本之后,提倡的容器配置方式从基于DTD的XML转向了基于 Schema的XML,进一步提高了配置方式的灵活性和可扩展性。同时,新的基于 Schema的配置方式为 Spring的AOP功能专门提供了独有的命名空间。
原来1x中基于DTD的AOP配置方式,可以稍微转换一下配置方式就移植到基于 Schema的AOP,所以,从这一点来说,基于 Schema的AOP只是配置方式的改变。当然,这只是从本质上说,稍后我们将看到,还有少许新的AOP特性实际上是可用的
简单来说xsd比dtd优点大多了,尽量替代dtd
-
@ Aspect形式AOP的折中用法。
要使用@ Aspect形式的AOP,必须要求使用Java5或者更高版本的JDK或JRE,因为注解是java5发布之后才引入的特性。如果我们不得不使用Java5之前的版本,而又想使用基于POJO的 Aspect声明方式,这时,就可以使用基于 Schema的 Spring AOP。使用基于 Schema的AOP,我们可以依然使用POJO声明 Aspect以及相关的 Advice。不过,不需要注解标注了,直接通过 Shema的配置文件进行配置就可以,@ AspectJ形式的 Pointcut表达式也全都可以配置到基于 Schema的配置文件中。
要使用基于 Schema的AOP,IoC容器的配置文件应该使用基于 Schema的XML,同时在文件头中增加针对AOP的命名空间声明,如:
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
参考:
《30–基于Schema的AOP》实战的一个例子
《Spring5参考指南:基于Schema的AOP》更多的语法说明