春季-无法在applicationContext.xml文件中导入persistence.xml

我正在使用eclipse juno IDE
我有具有src文件夹的Java应用程序.在文件夹中,我有:

1)applicationContext.xml

2)persistence.xml

我也有DBInterface,我用JPA实现了它.
现在,在applicationContext.xml文件中,有一个用于JPA实现的bean.

当我尝试注入bean时,我很兴奋地说类似“未找到持久性提供程序”.

所以我试图在applicationContext文件中导入持久性文件,但是又遇到了另一个异常.

applicationContext.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:aop="http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
   xmlns:context="http://www.springframework.org/schema/context/spring-context-2.5.xsd"
   xmlns:flow="http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd"
   xmlns:jms="http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
   xmlns:jee="http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
   xmlns:lang="http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"
   xmlns:osgi="http://www.springframework.org/schema/osgi/spring-osgi.xsd"
   xmlns:tx="http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
   xmlns:util="http://www.springframework.org/schema/util/spring-util-2.5.xsd"
   xmlns:p="http://www.springframework.org/schema/p"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd http://www.springframework.org/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd
      http://www.springframework.org/schema/jms/spring-jms-2.5.xsd http://www.springframework.org/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/lang/spring-lang-2.5.xsd http://www.springframework.org/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd
      http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/util/spring-util-2.5.xsd/spring-spring-util-2.5.xsd-2.5.xsd">


    <bean id="JPA"  class="pack.jpa.JPAQueries"/>
    <import resource="persistence.xml"/>
</beans>

persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>
<persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 

http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> 
         <persistence-unit transaction-type="RESOURCE_LOCAL" name="MyJPA"> 
             <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> 
             <class>pack.bl.Travels</class>
             <class>pack.bl.Example</class> 
             <properties> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/> 
                 <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/taxis"/> 
                 <property name="javax.persistence.jdbc.password" value=""/> 
                 <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
                 <property name="javax.persistence.jdbc.user" value="root"/>
             </properties> 
         </persistence-unit> 

mainClass

public class Program {

/**
 * @param args
 */
public static void main(String[] args) {

  ApplicationContext context = new  
  ClassPathXmlApplicationContext("applicationContext.xml");
  DBInterface dao = (DBInterface)context.getBean("JPA",JPAQueries.class);

  dao.retrieveRecords();
}

例外

Exception in thread "main"  
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration 
problem: Failed to import bean definitions from relative location [persistence.xml]

Offending resource: class path resource [applicationContext.xml]; nested exception is 
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration 
problem: Unable to locate Spring NamespaceHandler for XML schema namespace 
[http://java.sun.com/xml/ns/persistence]

Offending resource: class path resource [persistence.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:271)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:196)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:181)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:527)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:441)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at pack.program.Program.main(Program.java:16)

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://java.sun.com/xml/ns/persistence]

Offending resource: class path resource [persistence.xml]

at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:317)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1421)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:190)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:140)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:111)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:255)
... 20 more
最佳答案
您尝试将persistence.xml用作Spring配置绝对没有任何意义,因为persistence.xml不是Spring配置.

如果要将JPA与Spring一起使用,则需要将persistence.xml放入源文件夹中的META-INF文件夹中,并在applicationContext.xml中声明LocalContainerEntityManagerFactory:

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name = "persistenceUnitName" value = "MyJPA" />
</bean>

然后,您可以使用@PersistenceContext将EntityManager注入到Spring Bean中:

@PersistenceContext
private EntityManager em;

也可以看看:

> 13.5 JPA

转载注明原文:春季-无法在applicationContext.xml文件中导入persistence.xml - 代码日志