0

Why am I still getting this?

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../beans/Character.xml]
Offending resource: class path resource [spring/config/beanLocations.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [spring/beans/Character.xml]

        at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:72)

I've read many post about this issue, and configured my xml-s according to them.

My offending xml looks like this:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">

  <!-- Character Data Access Object -->
  <bean id="characterDao" class="com.got.common.dao.CharacterDao" >
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <tx:annotation-driven/>
  <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
</beans>

Spring-tx is included in my pom.xml:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

beanLocations.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
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
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd">


        <!-- Database Configuration -->
        <import resource="../database/DataSource.xml"/>
        <import resource="../database/hibernate.xml"/>

        <!-- Beans Declaration -->
        <import resource="../beans/Character.xml"/>

</beans>

assembly-plugin

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>com.got.common.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

I'm not sure that aop has to be included as well, but it's there. What's the problem with this namespace? I want to stop pulling my hair out so please tell me the reason this namespace can not be located.

ntj
  • 171
  • 12
  • have you confirmed spring-tx library is actually placed in your war file? – eis Dec 03 '17 at 09:13
  • also, what is in your `spring/config/beanLocations.xml`? some kind of nested definition? – eis Dec 03 '17 at 09:17
  • not, but the maven-assembly-plugin should put it in there. (It's a jar file.) I include `beanLocations.xml` in a sec. – ntj Dec 03 '17 at 09:18
  • Ok. Add your maven-assembly-plugin configuration as well, it might be relevant. There is nothing wrong with your xml configuration that you've posted so far, I suspect there is a problem with your build process instead, or then in beanLocations.xml. – eis Dec 03 '17 at 09:19
  • alright, another sec. – ntj Dec 03 '17 at 09:20
  • (you can check the contents of your .jar file by, for example, renaming it to a '.zip' and then open it with any zip tool.) – eis Dec 03 '17 at 09:21
  • I'm only able to open it in vim or do `jar tf ...`, and the result is 10k+ lines, but if i grep tx, i can see that `org/springframework/transaction/config/spring-tx.xsd org/springframework/transaction/config/spring-tx.gif` are in it. – ntj Dec 03 '17 at 09:24
  • I'm glad you showed me this link, and I'm going to try to solve this. – ntj Dec 03 '17 at 09:31
  • (moved the link to an answer) – eis Dec 03 '17 at 09:31

1 Answers1

1

The problem is probably caused by your use of assembly-plugin, which doesn't understand spring handler mechanism and overwrites the handler definitions in spring.handlers file. Behaviour is discussed in this thread: Idea to avoid that spring.handlers/spring.schemas get overwritten when merging multiple spring dependencies in a single jar

To avoid the problem, one option is to use shade plugin for your purposes like suggested here.

eis
  • 51,991
  • 13
  • 150
  • 199
  • I hope this answer will solve my problem, so I assume your solution can be accepted, thank you. – ntj Dec 03 '17 at 09:33
  • 1
    [this](https://issues.apache.org/jira/browse/MASSEMBLY-360) is the bug entry about assembly-plugin behaviour: the interesting part is that it was claimed fixed already in 2010, with test cases as well. It doesn't seem to be since people seem to still hit the issue, so example project proving the problem and commenting there could get it fixed in assembly plugin (where the fix belongs). They already have a filter for handling this, but I guess it just doesn't work properly. – eis Dec 03 '17 at 09:49
  • 1
    It works, can't thank You enough for this link after several hours of suffering. – ntj Dec 03 '17 at 11:15