Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring5和Hibernate5=>BeanDefinitionParsingException使用maven创建可执行jar时出现问题_Java_Spring_Hibernate_Maven_Executable Jar - Fatal编程技术网

Java 使用Spring5和Hibernate5=>BeanDefinitionParsingException使用maven创建可执行jar时出现问题

Java 使用Spring5和Hibernate5=>BeanDefinitionParsingException使用maven创建可执行jar时出现问题,java,spring,hibernate,maven,executable-jar,Java,Spring,Hibernate,Maven,Executable Jar,更新: 因为现在只有一个答案,我现在试着更好地描述它 简而言之,我的测试应用程序在eclipse中运行良好,并且在使用mvn exec启动时运行良好,但构建运行/工作的可执行jar包不适用于此项目 现在是更长的描述。我学习Java大约8个月,使用maven设置构建项目,并将Eclipse作为IDE进行编写和调试。目前,我正在尝试使用Spring、Hibernate和用于测试H2的一些小型数据库项目。DAO类使用entityManager。编译和运行我的测试应用程序在eclipse和maven中运

更新: 因为现在只有一个答案,我现在试着更好地描述它

简而言之,我的测试应用程序在eclipse中运行良好,并且在使用mvn exec启动时运行良好,但构建运行/工作的可执行jar包不适用于此项目

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

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

那很好。我的问题是开始构建并运行一个可执行jar。 通常我会在pom.xml中添加一个插件部分,例如:

   <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>
不幸的是,我在这个项目中遇到了问题。错误消息是:

org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:找不到XML架构命名空间的Spring NamespaceHandler[ 有问题的资源:类路径资源[Beans.xml]

由于项目正常运行,我预计构建可执行jar会有一些问题,并且我猜pom.xml在与bean定义的组合中缺少一些东西

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

这里是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

<?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>
我根据Slobodan Margetić的建议做了一些实验,谢谢:-但不幸的是,这没有帮助。更改Beans.xml导致应用程序完全停止工作。 由于我的测试应用程序在Eclipse和maven中工作,我希望Beans.xml应该是正确的,但是pom.xml可能会遗漏一些东西

我发现: 因为我在pom.xml中有Dependency spring tx,所以它应该正常工作。不幸的是它不工作。最后一个叫Anuja的人写道: 一些SpringJAR包含同名的元信息文件。”因此,为了避免某些元文件被覆盖,我使用maven shade插件来合并它们 也许我的项目也是如此。我如何检查它?
有没有人对maven shade插件有经验,或者有人建议我可以尝试找到什么解决方案?

尝试将此用于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


如果不起作用,请尝试通过此链接阅读:

谢谢您的回答。不幸的是,上面的更新没有帮助。我将尝试阅读并理解您发送的链接。
           <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>