在IDEA学习Spring项目运行时出现了以下报错
刚开始寻找问题源头关注在java.lang.IllegalStateException: Failed to load ApplicationContext上,上网查找解决方法则是说xml文件里面的bean初始化失败,尝试了许多方法都无效
接着寻找错误源头,发现了Caused by: org.xml.sax.SAXParseException; lineNumber: 37; columnNumber: 17; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 'aop:config' 的声明。
发现代码中只添加了xmlns:aop="http://www.springframework.org/schema/aop"
而没有在xsi:schemaLocation属性中声明与之对应的命名空间与xsd文件
我们只需要在xsi:schemaLocation后面加上
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
即可,需注意在两个地址间要有空格隔开
相信很多初入spring框架的新手都与我一样没有去关注xmlns、xmlns:xsi、xsi:schemaLocation 等元素的具体作用,这里贴上其他人对这些元素的解释
xmlns, xmlns:xsi, xsi:schemaLocation 解释 - feshfans - 博客园 (cnblogs.com)