Java 没有名为'的bean;持久性单位';定义
我正在开发一个spring jpa hibernate应用程序,但有以下例外:Java 没有名为'的bean;持久性单位';定义,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我正在开发一个spring jpa hibernate应用程序,但有以下例外: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addressDAO': Injection of persistence dependencies failed; nested exception is org.springframew
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addressDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'persistence-unit' is defined
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:357)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ma.jobcenter.dao.Main.main(Main.java:21)
这是我的spring application-context.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- tell spring where to find the beans -->
<context:component-scan base-package="ma.jobcenter.dao" />
<context:component-scan base-package="ma.jobcenter.service" />
<!-- tell spring to use annotation based congfigurations -->
<context:annotation-config/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- access -->
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/jobcenter-test-db" />
<property name="user" value="postgres" />
<property name="password" value="root" />
<!-- pool sizing -->
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="acquireIncrement" value="5" />
<property name="maxStatements" value="100" />
<!-- retries -->
<property name="acquireRetryAttempts" value="20" />
<property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
<property name="breakAfterAcquireFailure" value="false" />
<!-- refreshing connections -->
<property name="maxIdleTime" value="300" /> <!-- 5min -->
<property name="maxConnectionAge" value="300" /> <!-- 1h -->
<!-- timeouts and testing -->
<property name="testConnectionOnCheckout" value="false" />
<property name="testConnectionOnCheckin" value="false" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="ma.jobcenter.domain.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="addressDAO" class="ma.jobcenter.dao.AddressDAOImpl"/>
<bean id="candidateDAO" class="ma.jobcenter.dao.CandidateDAOImpl"/>
</beans>
应用程序正在Tomcat上运行。
我真的不明白这个例外,也没有找到任何答案。
提前感谢。尝试将您的
persistence.xml
文件放入src/main/resources/META-INF
中,我想您应该删除应用程序上下文中的
行
还要检查src/resources
中的所有文件是否都复制到部署到Tomcat的WAR
文件中。尝试将persistence.xml
文件放入src/main/resources/META-INF
中,我想您应该删除应用程序上下文.xml中的
行
还要检查src/resources
中的所有文件是否都复制到部署到Tomcat的WAR
文件中。我找到了解决方案,我必须在@PersistenceContext中通过name=“persistence unit”更改unitName=“persistence unit”,现在一切正常。我找到了解决方案,我必须通过名称更改unitName=“persistence unit”=@PersistenceContext中的“persistence unit”现在可以正常工作。您将“persistence.xml”文件放在哪里了?在我的maven src/main/resources目录中?您将“persistence.xml”文件放在哪里了?在我的maven src/main/resources目录中
public abstract class AbstractDAO {
@PersistenceContext(unitName = "persistence-unit")
protected EntityManager entityManager;
public AbstractDAO() {
super();
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- tell spring where to find the beans -->
<context:component-scan base-package="ma.jobcenter.dao" />
<context:component-scan base-package="ma.jobcenter.service" />
<!-- tell spring to use annotation based congfigurations -->
<context:annotation-config/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!-- access -->
<property name="driverClass" value="org.postgresql.Driver" />
<property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/jobcenter-test-db" />
<property name="user" value="postgres" />
<property name="password" value="root" />
<!-- pool sizing -->
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="acquireIncrement" value="5" />
<property name="maxStatements" value="100" />
<!-- retries -->
<property name="acquireRetryAttempts" value="20" />
<property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
<property name="breakAfterAcquireFailure" value="false" />
<!-- refreshing connections -->
<property name="maxIdleTime" value="300" /> <!-- 5min -->
<property name="maxConnectionAge" value="300" /> <!-- 1h -->
<!-- timeouts and testing -->
<property name="testConnectionOnCheckout" value="false" />
<property name="testConnectionOnCheckin" value="false" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="ma.jobcenter.domain.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
</bean>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="addressDAO" class="ma.jobcenter.dao.AddressDAOImpl"/>
<bean id="candidateDAO" class="ma.jobcenter.dao.CandidateDAOImpl"/>
</beans>