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

编程入门 行业动态 更新时间:2024-10-21 16:23:00
本文介绍了无法在applicationContext.xml文件中导入persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm using eclipse juno IDE I have Java application which have src folder. within the folder I have:

1)applicationContext.xml

1) applicationContext.xml

2)persistence.xml

2) persistence.xml

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

I also have DBInterface and i implemented it with JPA. Now in the applicationContext.xml file I have a bean for the JPA implemention.

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

When I tried to inject the bean i got an excpetion that said something like "No Persistence Provider was found".

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

So I tried to import the persistence file within the applicationContext file but i'm getting another exception.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:aop="www.springframework/schema/aop/spring-aop-2.5.xsd" xmlns:context="www.springframework/schema/context/spring-context-2.5.xsd" xmlns:flow="www.springframework/schema/webflow-config/spring-webflow-config-1.0.xsd" xmlns:jms="www.springframework/schema/jms/spring-jms-2.5.xsd" xmlns:jee="www.springframework/schema/jee/spring-jee-2.5.xsd" xmlns:lang="www.springframework/schema/lang/spring-lang-2.5.xsd" xmlns:osgi="www.springframework/schema/osgi/spring-osgi.xsd" xmlns:tx="www.springframework/schema/tx/spring-tx-2.5.xsd" xmlns:util="www.springframework/schema/util/spring-util-2.5.xsd" xmlns:p="www.springframework/schema/p" xsi:schemaLocation="www.springframework/schema/beans www.springframework/schema/beans/spring-beans-2.5.xsd www.springframework/schema/aop/spring-aop-2.5.xsd www.springframework/schema/aop/spring-aop-2.5.xsd/spring-spring-aop-2.5.xsd-2.5.xsd www.springframework/schema/context/spring-context-2.5.xsd www.springframework/schema/context/spring-context-2.5.xsd/spring-spring-context-2.5.xsd-2.5.xsd www.springframework/schema/webflow-config/spring-webflow-config-1.0.xsd www.springframework/schema/webflow-config/spring-webflow-config-1.0.xsd/spring-spring-webflow-config-1.0.xsd-2.5.xsd www.springframework/schema/jms/spring-jms-2.5.xsd www.springframework/schema/jms/spring-jms-2.5.xsd/spring-spring-jms-2.5.xsd-2.5.xsd www.springframework/schema/jee/spring-jee-2.5.xsd www.springframework/schema/jee/spring-jee-2.5.xsd/spring-spring-jee-2.5.xsd-2.5.xsd www.springframework/schema/lang/spring-lang-2.5.xsd www.springframework/schema/lang/spring-lang-2.5.xsd/spring-spring-lang-2.5.xsd-2.5.xsd www.springframework/schema/osgi/spring-osgi.xsd www.springframework/schema/osgi/spring-osgi.xsd/spring-spring-osgi.xsd-2.5.xsd www.springframework/schema/tx/spring-tx-2.5.xsd www.springframework/schema/tx/spring-tx-2.5.xsd/spring-spring-tx-2.5.xsd-2.5.xsd www.springframework/schema/util/spring-util-2.5.xsd www.springframework/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="java.sun/xml/ns/persistence java.sun/xml/ns/persistence/persistence_1_0.xsd" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns="java.sun/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 [java.sun/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 [java.sun/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配置.

Your attempt to use persistence.xml as a Spring config makes absolutely no sense, because persistence.xml is not a Spring config.

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

If you want to use JPA with Spring, you need to put persistence.xml into META-INF folder inside your source folder, and declare LocalContainerEntityManagerFactory in applicationContext.xml:

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

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

Then you can inject EntityManager into your Spring bean using @PersistenceContext:

@PersistenceContext private EntityManager em;

另请参见:

  • 13.5 JPA

更多推荐

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

本文发布于:2023-10-25 14:03:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   applicationContext   xml   persistence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!