2

我正在创建一个基本的 spring-maven 项目,该项目应该作为 java 应用程序运行(在进程中,而不是通过 Web 服务器)。

我的应用程序上下文位于我的类路径中的资源文件夹下:

<?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:tx="http://www.springframework.org/schema/tx" xmlns:ox="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1xsd">


<!-- Root Context: defines shared resources visible to all other web components -->

<!-- Context -->
<context:component-scan base-package="me.co.nutrition" />

<!-- Properties -->
<context:property-placeholder
    location="classpath:/nutrition.accumulation.properties" />

<!-- DB -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="persistenceUnitManager"
    class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="defaultPersistenceUnitName" value="nutrition-pu"/>
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
    <property name="defaultDataSource" ref="dataSource" />
</bean>

<!-- JPA -->
<bean id="entityManager"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitManager" ref="persistenceUnitManager" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>

<!-- jpaVendorAdapter (works in conjunction with the persistence.xml) -->
<bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="${jpa.database}" />
    <property name="showSql" value="${jpa.showSql}" />
    <property name="databasePlatform" value="${jpa.dialect}" />
    <property name="generateDdl" value="${jpa.generateDdl}" />
</bean>

<!-- Transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManager" />
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

</beans>

我创建了一个 META-INF 文件夹作为源文件夹,并将这个文件夹添加到我的类路径中。我在这个文件夹下创建了我的 persistence.xml 文件。

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

<persistence-unit name="nutrition-pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
</persistence>

如前所述,它位于我的类路径中的 META-INF 文件夹下。这是我的 .classpath 文件:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java"/>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
    <classpathentry kind="src" path="META-INF"/>
    <classpathentry kind="src" path="resources"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

您可以看到 META-INF 文件夹在那里。我创建了一个简单的 main 运行:

public static void main(String[] args) throws Exception {

            ApplicationContext context = new ClassPathXmlApplicationContext("accumulationApplicationContext.xml");

...
}

当我运行它时,当我尝试加载应用程序上下文时,我得到一个异常,指出无法找到或解析persistence.xml:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in class path resource [accumulationApplicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot parse persistence unit from class path resource [META-INF/persistence.xml]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 30 more
Caused by: java.lang.IllegalArgumentException: Cannot parse persistence unit from class path resource [META-INF/persistence.xml]
    at org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:141)
    at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.readPersistenceUnitInfos(DefaultPersistenceUnitManager.java:380)
    at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:341)
    at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:326)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 37 more
Caused by: java.io.FileNotFoundException: class path resource [META-INF/persistence.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
    at org.springframework.orm.jpa.persistenceunit.PersistenceUnitReader.readPersistenceUnitInfos(PersistenceUnitReader.java:129)

我无法理解这个问题。我很确定我有正确的结构,为什么我的 persistence.xml 不能加载?

提前致谢!

更新:

感谢鲍里斯,我得到了它的工作。似乎我在错误的路径下创建了 META-INF。发生这种情况是因为我错过了对 Eclipse 项目结构的理解。是正确的结构。我希望这可以帮助别人..

4

1 回答 1

6

您的 META-INF 文件夹应该直接位于源文件夹,而不是单独的源文件夹。

请参阅我应该在 Eclipse 中将 META-INF 放在哪里?

从 JPA 2.0 规范更新

8.2 <...> 持久性单元由persistence.xml 文件定义。META-INF 目录包含persistence.xml 文件的jar 文件或目录称为持久单元的根。在 Java EE 环境中,持久性单元的根必须是以下之一:

  • 一个 EJB-JAR 文件
  • WAR 文件的 WEB-INF/classes 目录
  • WAR文件的WEB-INF/lib目录下的jar文件
  • EAR 库目录中的 jar 文件
  • 应用程序客户端 jar 文件

8.2.1 <...> persistence.xml 文件定义了一个持久性单元。persistence.xml 文件位于 持久单元根目录的META-INF目录中。

因此,如何配置项目没有区别,重要的是生成的 jar。如果您使用的是 maven,通常的做法是使用src/main/resources文件夹作为 META-INF。

于 2012-08-30T08:15:43.557 回答