38

My setup is fairly simple: I have a web front-end, back-end is spring-wired.

I am using AOP to add a layer of security on my rpc services.

It's all good, except for the fact that the web app aborts on launch:

  [java] SEVERE: Context initialization failed
     [java] org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop]
     [java] Offending resource: ServletContext resource [/WEB-INF/gwthandler-servlet.xml]

Here is the snippet from my xml config file:

<beans 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.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <aop:config>
        <aop:aspect id="security" ref="securityAspect" >
            <aop:pointcut id="securedServices" expression="@annotation(com.fb.boog.common.aspects.Secured)"/>
            <aop:before method="checkSecurity" pointcut-ref="securedServices"/>
        </aop:aspect>
    </aop:config>

I read over the internets that it may be my classloading the core of the problem. Doubtful, since here is my WEB-INF/lib directory:

./WEB-INF/lib
./WEB-INF/lib/aopalliance-alpha1.jar
./WEB-INF/lib/aspectj-1.6.6.jar
./WEB-INF/lib/commons-collections.jar
./WEB-INF/lib/commons-logging.jar
./WEB-INF/lib/ehcache-core-1.7.0.jar
./WEB-INF/lib/ejb3-persistence.jar
./WEB-INF/lib/hibernate
./WEB-INF/lib/hibernate/antlr.jar
./WEB-INF/lib/hibernate/asm.jar
./WEB-INF/lib/hibernate/bsh-2.0b1.jar
./WEB-INF/lib/hibernate/cglib.jar
./WEB-INF/lib/hibernate/dom4j.jar
./WEB-INF/lib/hibernate/freemarker.jar
./WEB-INF/lib/hibernate/hibernate-annotations.jar
./WEB-INF/lib/hibernate/hibernate-shards.jar
./WEB-INF/lib/hibernate/hibernate-tools.jar
./WEB-INF/lib/hibernate/hibernate.jar
./WEB-INF/lib/hibernate/jtidy-r8-20060801.jar
./WEB-INF/lib/jabsorb
./WEB-INF/lib/jabsorb/jabsorb-1.3.1.jar
./WEB-INF/lib/jta.jar
./WEB-INF/lib/jyaml-1.3.jar
./WEB-INF/lib/postgresql-8.4-701.jdbc4.jar
./WEB-INF/lib/sjsxp.jar
./WEB-INF/lib/spring
./WEB-INF/lib/spring/org.springframework.aop-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.asm-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.aspects-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.beans-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.context-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.context.support-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.core-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.expression-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.instrument-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.instrument.tomcat-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.jdbc-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.jms-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.orm-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.oxm-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.test-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.transaction-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.web-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.web.portlet-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.web.servlet-3.0.0.RELEASE.jar
./WEB-INF/lib/spring/org.springframework.web.struts-3.0.0.RELEASE.jar
./WEB-INF/lib/testng-5.11-jdk15.jar
./WEB-INF/web.xml
skaffman
  • 398,947
  • 96
  • 818
  • 769
Nick Hristov
  • 905
  • 1
  • 8
  • 17

11 Answers11

48

Encountered this error while using maven-shade-plugin, the solution was including:

META-INF/spring.schemas

and

META-INF/spring.handlers

transformers in the maven-shade-plugin when building...

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.handlers</resource>
                        </transformer>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>META-INF/spring.schemas</resource>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>

(Credits: Idea to avoid that spring.handlers/spring.schemas get overwritten when merging multiple spring dependencies in a single jar)

Community
  • 1
  • 1
xgMz
  • 3,334
  • 2
  • 30
  • 23
  • this was it for me too. As a reference I'm building a stand alone Apache Camel route based on the maven archetype camel spring dsl . – Miguel Coquet Mar 22 '13 at 10:09
16

http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

I ran into a similar problem using the maven-shade-plugin. I found the solution to my problems in their example page above.

Kevin
  • 169
  • 1
  • 4
  • A comment in the bug related to the assembly plugin points to the shade plugin working. I converted to the shade plugin and had everything running again in no time. http://jira.codehaus.org/browse/MASSEMBLY-360 – digitaljoel Apr 30 '12 at 04:24
12

What IDE (if any) are you using? Does this happen when you're working within an IDE, or only on deployment? If it's deployment, it might be because whatever mechanism of deployment you use -- maven-assembly making a single JAR with dependencies is a known culprit -- is collapsing all your JARs into a single directory and the Spring schema and handler files are overwriting each other.

delfuego
  • 14,085
  • 4
  • 39
  • 39
  • Using IntelliJ for an IDE. No problems in the IDE, everything checks out. I am using ant to assemble the web app, and the listing above is from the war directory. – Nick Hristov Dec 21 '09 at 02:06
  • +1 to delfuego for linking to MASSEMBLY-360; that's exactly the problem I'm having. – Andrew Swan Mar 11 '10 at 03:38
  • 1
    If using Spring 2.5.6, you can work around this by using the all-in-one "spring" JAR file that comes with that version (as opposed to separately using spring-core, spring-beans, spring-jdbc, etc.) – Andrew Swan Mar 11 '10 at 04:02
  • Is there an easy fix for this problem? – DD. Jan 27 '12 at 18:31
  • +1 to delfuego and Andrew Swan both. Circa 2012 I ran into the same thing. – sparc_spread Mar 14 '12 at 13:50
  • @DD. as suggested in the discussion of the bug, one solution could be to use the one-jar library, available as a Maven plug-in. Works for me. – Giulio Piancastelli May 31 '12 at 10:29
9

Did you try putting all your jars directly in the WEB-INF/lib dir instead of sub-dirs of that?

No WEB-INF/lib/spring/org.springframework.aop-3.0.0.RELEASE.jar, just WEB-INF/lib/org.springframework.aop-3.0.0.RELEASE.jar

Same with the rest of the jars.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Peter Lynch
  • 525
  • 5
  • 9
  • Yep -- according to this, subdirectories under `WEB-INF/lib` aren't valid. Never knew one way or another how that'd work... nice to know now. http://code.google.com/appengine/docs/java/tools/ant.html#Copying_JARs – delfuego Dec 21 '09 at 03:39
  • By the way, I suspect that the reason it works fine in IntelliJ is that IntelliJ is using a custom classloader... – delfuego Dec 21 '09 at 03:39
  • 1
    Yes, that fixed it. IntelliJ does not complain , because it has the jars added in directly under the libraries section. – Nick Hristov Dec 21 '09 at 11:26
  • One more thing to note, ant's copy task has a "flatten" argument, which is perfect for this task. – Nick Hristov Nov 11 '10 at 18:05
  • once the class are in class path all the class should be available as long as package name is correct. folder name should not matter. so this is an issue with IDE.. please suggest if i am wrong. – Punith Raj Aug 19 '13 at 10:09
  • Refer this post for detailed info: http://www.baeldung.com/unable-to-locate-spring-namespacehandler-for-xml-schema-namespace – Atul Aug 18 '14 at 03:47
1

I ran into a similar error, but refering to Spring Webflow in a newly created Roo project. The solution for me turned out to be (Project) / right-click / Maven / Enable Maven Dependencies (followed by some restarts and republishes to Tomcat).

It appeared that STS or m2Eclipse was failing to push all the spring webflow jars into the web app lib directory. I'm not sure why. But enabling maven dependency handling and then rebuilding seemed to fix the problem; the webflow jars finally get published and thus it can find the schema namespace references.

I investigated this by exploring the tomcat directory that the web app was published to, clicking into WEB-INF/lib/ while it was running and noticing that it was missing webflow jar files.

skywind
  • 11
  • 1
1

This trick worked for me too: In Eclipse right-click on the project and then Maven > Update Dependencies.

Community
  • 1
  • 1
javasun
  • 19
  • 1
0

In case someone else runs into this problem, I just did using Eclipse; running the project via the right click action. This error occurred in the J2EE view, but did NOT occur in the Java view. Not sure - assuming something with adding libraries to the correct 'lib' directory.

I am also using a Maven project, allowing m2eclipse to manage dependancies.

wuntee
  • 12,170
  • 26
  • 77
  • 106
0

I have the same problem with spring 3.0.2 and spring-beans-3.0.xsd.

My solution:

Create a file META-INF/spring.schemas in the source folder and copy all necesary definitions. Create spring.handlers too.

I think that the class PluggableSchemaResolver is not working correctly.

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/xml/PluggableSchemaResolver.html

from the javadoc:

"By default, this class will look for mapping files in the classpath using the pattern: META-INF/spring.schemas allowing for multiple files to exist on the classpath at any one time."

but in my case, this class only read the first spring.schemas finded.

Grettings. pacovr

opyate
  • 5,388
  • 1
  • 37
  • 64
  • +1 for mentioning PluggableSchemaResolver. Here's a good article on the subject: http://denis-zhdanov.blogspot.com/2010/01/spring-xsd-loading-algorithm.html – opyate Nov 07 '11 at 17:20
  • yes, thank you, merging all META-INF/spring.schemas (the same for META-INF/spring.handlers) in all spring jars is the solution for me. – wxl24life Dec 15 '14 at 01:47
0

Make sure you have all dependencies solved

I run into this problem in my first attempt at AOP, following a spring tutorial. My problem was not having spring-aop.jar in my classpath. The tutorial listed all other dependencies I had to add, namely:

  • aspectjrt.jar
  • aspectjweaver.jar
  • aspectj.jar
  • aopalliance.jar

But the one was missing. Just one more problem that can contribute to that symptom in the original question.

I am using Eclipse (neon), Java SE 8, beans 3.0, spring AOP 3.0, Spring 4.3.4. The problem showed in the Java view --not JEE--, and while trying to just run the application with Right button menu -> Run As -> Java Application.

manuelvigarcia
  • 1,696
  • 1
  • 22
  • 32
0

If using mvn, check that you have the correct scope (if you have that defined) in your pom.xml. I once had it incorrectly set to test but needed it for runtime.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54
panksdmz
  • 421
  • 1
  • 5
  • 8
-1

You can also try using the one-jar maven plugin which fixed the problem for us. Simply follow the instructions from here.

Johan
  • 37,479
  • 32
  • 149
  • 237