使用Spring 5和Hibernate 5使用Maven创建可执行jar时出现问题.BeanDefinitionParsingException [英] problem creating an executable jar with maven using spring 5 and hibernate 5 => BeanDefinitionParsingException

查看:65
本文介绍了使用Spring 5和Hibernate 5使用Maven创建可执行jar时出现问题.BeanDefinitionParsingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:由于目前只有一个答案,所以我现在尝试更好地描述它.

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

简而言之,我的测试应用程序在eclipse中运行良好,并且从mvn exec开始运行,但构建运行/工作的可执行jar包对该项目不起作用.

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.

现在是更长的描述.我学习Java大约8个月,并使用maven设置构建项目,并使用Eclipse作为IDE进行编写和调试.目前,我正在尝试使用Spring,Hibernate进行一些小型数据库项目,并用于测试H2.DAO类使用entityManager.在Eclipse和Maven中,编译和运行我的测试应用程序都可以正常工作.在月食中,我使用:

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"

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

那很好.我的问题开始建立并运行可执行jar.通常我习惯将插件部分添加到pom.xml中,例如:

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>

当maven exec命令工作并看起来像例如

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:配置问题:无法找到XML模式命名空间的Spring NamespaceHandler [

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]

随着项目正常运行,我希望构建可执行jar会遇到一些问题,而且我猜pom.xml与bean定义结合在一起会丢失某些东西.

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.

现在有两个文件pom.xml和Beans.xml.

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

这是我的pom.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>

这是我的Beans.xml

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>

我根据SlobodanMargetić(谢谢:-)的建议进行了一些实验,但不幸的是,它没有帮助.更改Beans.xml会使应用程序完全停止运行.当我的测试应用程序在Eclipse和Maven中工作时,我希望Beans.xml应该正确,但是希望pom.xml会丢失某些东西.

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.

我发现了这一点: https://www.baeldung.com/无法找到spring-namespacehandler-xml-schema-namespace 由于我的pom.xml中具有从属spring-tx,因此它通常可以正常工作.不幸的是,它不起作用.最后,一个叫Anuja的人写道:有些弹簧罐包含名称相同的元信息文件."因此,为避免覆盖某些元文件,我使用了maven-shade-plugin来合并它们,也许我的项目也是如此.我该如何检查?有没有使用 maven-shade-plugin 的经验或对我可以找到解决方案的建议?

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?

推荐答案

尝试将其用于bean:

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>

应该使它遍历文件以查找bean.

it should make it go through files to find beans.

如果它不起作用,请尝试阅读以下链接: http://www.mastertheboss.com/jboss-frameworks/cdi/configuring-beans-xml-file

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

这篇关于使用Spring 5和Hibernate 5使用Maven创建可执行jar时出现问题.BeanDefinitionParsingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆