Im keep getting this error: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop]

Im keep getting this error: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop]

我目前正在学习 Spring。我遇到的错误似乎很常见,我已经阅读了很多 post 处理相同问题的文章。我已将依赖项添加到我的 pom.xml 并将模式添加到我的 Beans.xml。我使用的版本是相同的,并且在全新安装后,aop 出现在一个 jar 文件中。当我 运行 一个 jar 文件时,我最终遇到了同样的错误。

我还能做些什么来解决这个问题?

(必须截屏,Whosebug 不希望我将 xml 代码添加到我的 post)

堆栈跟踪:

Mar 26, 2022 10:38:46 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5fcfe4b2: startup date [Sat Mar 26 10:38:46 CET 2022]; root of context hierarchy
Mar 26, 2022 10:38:46 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop]
Offending resource: class path resource [Beans.xml]

        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
        at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:316)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1421)
        at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1414)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:189)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:143)
        at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:110)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
        at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
        at com.packs.app.App.main(App.java:9)
<?xml version="1.0" encoding="UTF-8"?>
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-4.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<bean id="knight" class="com.packs.beans.BraveKnight">
    <constructor-arg ref="quest" />
</bean>
<bean id="quest" class="com.packs.beans.SlayDragonQuest">
    <constructor-arg value="#{T(System).out}" />
</bean>
<bean id="minstrel" class="com.packs.aspect.Minstrel">
    <constructor-arg value="#{T(System).out}" />
</bean>
<aop:config>
<aop:aspect ref="minstrel">
        <aop:pointcut id="embark" expression="execution(* *.embarkOnQuest(..))"/>
        <aop:before pointcut-ref="embark" method="singBeforeQuest"/>
        <aop:after pointcut-ref="embark" method="singAfterQuest"/>
    </aop:aspect>
</aop:config>

</beans>

问题似乎出在您决定 而不是 到 post、Beans.xml 的文件中 - 为什么 upper-case "B “, 顺便一提?也许你错过了像

这样的东西
xmlns:aop="http://www.springframework.org/schema/aop"

and/or

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd

在您的 <beans ...> 声明中,例如在一个工作案例中可能看起来像这样,这取决于你想在你的 Spring 项目中使用和配置什么样的东西:

<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/tx/spring-tx.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
  "
>

在 OP posted a link 到他的 GitHub 项目后更新: 我克隆了你的项目并且 运行它。你仍然有同样的错误是不正确的。 XML 文件现在可以使用了。我看到的新错误是(我添加了一些换行符):

org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'knight' defined in class path resource [Beans.xml]:
  BeanPostProcessor before instantiation of bean failed; nested exception is
    org.springframework.beans.factory.BeanCreationException:
    Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0':
    Cannot create inner bean '(inner bean)#4f6ee6e4' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is
      org.springframework.beans.factory.BeanCreationException:
      Error creating bean with name '(inner bean)#4f6ee6e4':
      Cannot resolve reference to bean 'embark' while setting constructor argument; nested exception is
        org.springframework.beans.factory.BeanCreationException:
        Error creating bean with name 'embark': Instantiation of bean failed; nested exception is
          java.lang.NoClassDefFoundError:
          org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
(...)

所以 Spring 无法从 AspectJ 编织器中找到 class,它必须在您的 class 路径上才能使 Spring AOP 工作。如果查看 spring-aop-4.1.0.RELEASE.pom,您将看到:

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.2</version>
  <scope>compile</scope>
  <optional>true</optional>
</dependency>

范围optional!这意味着,您必须明确地在自己的 POM 中声明依赖关系,才能使其位于 class 路径上。将此添加到您的 POM(让我们使用最新的 1.8.x 版本):

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.14</version>
</dependency>

现在您的应用程序运行良好并打印:

Fa la la, the knight is so brave!
Embarking on quest to slay the dragon!
Tee hee hee, the brave knight did embark on a quest!

顺便说一句,因为你似乎刚从 Spring 开始,我建议你使用更新的版本(也许还有 Spring Boot),也许更愿意了解 annotation-driven 配置比 XML-defined 一个,尽管如果您必须处理遗留应用程序,那么了解两者绝对是件好事。但首先,您可以按照教程学习基础知识。实际上,没有任何容器或应用程序服务器的纯 Spring 应用程序的启动速度比例如Spring 启动应用程序。