Spring MVC配置aop:scoped-proxy报cvc-complex-type.2.4.c错误咨询
Spring项目XML配置触发aop元素识别异常的问题排查
问题背景
项目为集成JPA、以Oracle为数据库的Spring MVC工程,在servlet.xml配置文件中编写bean声明逻辑时,运行测试用例抛出XML校验异常,相关配置与错误信息如下:
现有配置片段
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="bbtra" class="com.bonanza.commons.services.TranslationDB" autowire-candidate="true"> <property name="dataSource" ref="dataSource" /> <aop:scoped-proxy/> </bean>
报错信息
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:scoped-proxy'.
问题产生原因
问题根源在于xsi:schemaLocation内部的命名空间与XSD地址配对错乱:
- schemaLocation的标准格式要求为「命名空间URI 对应XSD文件地址」成对出现,相邻两个字符串为一组配对生效
- 原配置中写完
http://www.springframework.org/schema/tx命名空间后,本应紧跟tx模块对应的xsd地址,却错误将aop模块的命名空间URI作为tx的xsd值,直接打断了后续aop命名空间的xsd加载链路 - 配对错乱后,XML解析器无法获取aop模块的合法元素定义,自然识别不出
<aop:scoped-proxy>的结构合法性,最终在严格校验模式下抛出元素未声明的报错。
解决方法
- 第一,调整schemaLocation内的条目顺序,修正所有配对关系,保证每个在根标签xmlns中声明的命名空间,都匹配到对应的xsd地址,修正后的schemaLocation配置为:
xsi:schemaLocation=" http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd " - 第二,核对项目依赖,确保已经引入与当前Spring版本匹配的
spring-aop组件,避免XML校验通过后,运行阶段出现aop相关类缺失的连锁异常。
内容的提问来源于stack exchange,提问作者Nunyet Calçada
相关产品推荐
相关产品推荐

