problem creating an executable jar with maven using spring 5 and hibernate 5 => BeanDefinitionParsingException

随声附和 提交于 2021-02-11 13:48:18

问题


Update: As there is only one answer for now I try to describe it better now.

In short words, my test application runs fine in eclipse and when starting with mvn exec but building a running/working executable jar package does not work for this project.

Now the longer description. I'm learning Java for about 8 month and use maven to setup build projects and Eclipse as IDE for writing and debugging. Currently I am trying to make some small database projects using Spring, Hibernate and for testing H2. The DAO classes use the entityManager. Compiling and running my test application works fine in eclipse and in maven. In eclipse I use:

mvn exec:java -Dexec.mainClass="maven.springhibernateh2.basic.CRUDTest"

That works fine. My problem started to build and run an executable jar. Normally I am used to add a plugin section into the pom.xml like e.g.:

   <plugin>
     <artifactId>maven-assembly-plugin</artifactId>
     <version>3.2.0</version>
     <configuration>
       <descriptorRefs>
         <descriptorRef>jar-with-dependencies</descriptorRef>
       </descriptorRefs>
       <archive>
         <manifest>
           <mainClass>maven.springhibernateh2.basic.CRUDTest</mainClass>
         </manifest>
       </archive>
     </configuration>
     <executions>
       <execution>
         <id>make-assembly</id>
         <phase>package</phase>
         <goals>
           <goal>single</goal>
         </goals>
       </execution>
     </executions>
   </plugin>

That's normally enough when the maven exec command works and looks like e.g.

mvn exec:java -Dexec.mainClass="maven.springhibernateh2.basic.CRUDTest"

Unfortunately I get problems in this project. The error message 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 [Beans.xml]

As the projects normally runs I expect that building the executable jar has some problems and I guess that the pom.xml is missing something in combinations with the beans definition.

Now the two files pom.xml and Beans.xml.

Here my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>roland.egger</groupId>
  <artifactId>maven.springhibernateh2.basic</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
               <source>1.8</source>
               <target>1.8</target>
            </configuration>
         </plugin>
         <plugin>
           <artifactId>maven-assembly-plugin</artifactId>
           <version>3.2.0</version>
           <configuration>
             <descriptorRefs>
               <descriptorRef>jar-with-dependencies</descriptorRef>
             </descriptorRefs>
             <archive>
               <manifest>
                 <mainClass>maven.springhibernateh2.basic.CRUDTest</mainClass>
               </manifest>
             </archive>
           </configuration>
           <executions>
             <execution>
               <id>make-assembly</id>
               <phase>package</phase>
               <goals>
                 <goal>single</goal>
               </goals>
             </execution>
           </executions>
         </plugin>
      </plugins>
   </build>
   <properties>
      <slf4j.version>1.7.30</slf4j.version>
      <spring.version>5.2.5.RELEASE</spring.version>
      <hibernate.version>5.4.12.Final</hibernate.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-core</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-context</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-orm</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aspects</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-aop</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-tx</artifactId>
         <version>${spring.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
      <dependency>
         <groupId>com.h2database</groupId>
         <artifactId>h2</artifactId>
         <version>1.4.200</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
      <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-core</artifactId>
         <version>${hibernate.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
      <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-entitymanager</artifactId>
         <version>${hibernate.version}</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
      <dependency>
         <groupId>org.hibernate.javax.persistence</groupId>
         <artifactId>hibernate-jpa-2.1-api</artifactId>
         <version>1.0.2.Final</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>${slf4j.version}</version>
      </dependency>
      <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>${slf4j.version}</version>
      </dependency>
      <!-- Fuer den RollingFileAppender -->
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>apache-log4j-extras</artifactId>
         <version>1.1</version>
      </dependency>
   </dependencies>
</project>

Here is my Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

   <context:component-scan base-package="maven.springhibernateh2.basic"></context:component-scan>
        <bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName"
                        value="${db.driverClassName}"></property>
                <property name="url" value="${db.url}"></property>
                <property name="username" value="${db.username}"></property>
                <property name="password" value="${db.password}"></property>
        </bean>


        <bean
                class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
                <property name="locations">
                        <list>
                                <value>database.properties</value>
                        </list>
                </property>
                <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>


   <!-- Definition des JpaTransactionManagers -->
   <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>

   <!-- Activation of @Transactional Annotation. For a simple example without aspectj mode to
        reduce the dependency complexity -->
   <tx:annotation-driven transaction-manager="transactionManager" />

   <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
      <property name="persistenceUnitName" value="roland.egger.maven.springhibernate" />
      <property name="dataSource" ref="dataSource" />
   </bean>
    <!-- The next line is important making the proxy working -->
    <aop:config proxy-target-class="true"/>

</beans>

I did some experiments with the suggestion of Slobodan Margetić (thank you :-) ) but unfortunately it did not help. Changing the Beans.xml caused the application to stop working at all. As my test application worked in Eclipse and in maven I expect that the Beans.xml should be correct but expect the pom.xml to miss something.

I found this: https://www.baeldung.com/unable-to-locate-spring-namespacehandler-for-xml-schema-namespace As I have the dependeny spring-tx in my pom.xml it should normally work. Unfortunately it does not work. At the end a person called Anuja wrote: "Some of the spring jars contain meta info files with the same name.” Hence to avoid that some meta files are overridden, I used the maven-shade-plugin to merge them " Maybe this can be the case for my project, too. How can I check it? Has anybody experience with the maven-shade-plugin or a suggestion what I can try to find a solution?


回答1:


try using this for beans:

           <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                               http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" bean-discovery-mode="all" version="2.0">
           </beans>

it should make it go through files to find beans.

if it doesnt work try reading through this link : http://www.mastertheboss.com/jboss-frameworks/cdi/configuring-beans-xml-file



来源:https://stackoverflow.com/questions/61817181/problem-creating-an-executable-jar-with-maven-using-spring-5-and-hibernate-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!