Search code examples
xmlspringnamespacesaop

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


I'm currently learning Spring. The error I'm getting seems to be a common one and I've read a lot of posts dealing with the same problem. I've added the dependency to my pom.xml and the schema to my Beans.xml. The versions I'm using are the same, and after a clean install the aop is present in the one-jar file. When I run the one-jar file, I end up with the same error.

Any ideal what more I can do to fix this?

enter image description here

(had to make a screenshot, stackoverflow did not want me to add the xml code to my post)

The stack trace:

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>

Solution

  • The problem seems to be in the very file which you decided not to post, Beans.xml - why the upper-case "B", by the way? Maybe you are missing something like

    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
    

    in your <beans ...> declaration, which e.g. could look like this in a working case, depending on what kind of stuff you want to use and configure in your Spring project:

    <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
      "
    >
    

    Update after the OP posted a link to his GitHub project: I cloned your project and ran it. It is not true that you still have the same error. The XML file works now. The new error I see reads (I added some line breaks):

    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
    (...)
    

    So Spring cannot find a class from AspectJ weaver, which must be on your classpath for Spring AOP to work. If you look into the spring-aop-4.1.0.RELEASE.pom, you will see:

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

    Scope optional! That means, you explicitly have to declare the dependency in your own POM for it to be on the class path. Add this to your POM (let us use the latest 1.8.x version):

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

    Now your application works like a charm and prints:

    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!
    

    BTW, as you seem to just start with Spring, I recommend you to use a more recent version (maybe also Spring Boot) and maybe rather learn about annotation-driven configuration than XML-defined one, even though it is definitely good to know both, if you have to deal with legacy applications. But first, you can follow your tutorial and learn the basics. Actually, a pure Spring application without any container or application server starts much more quickly than e.g. a Spring Boot app.