gpt4 book ai didi

java - 未定义名为 'persistence-unit' 的 bean

转载 作者:行者123 更新时间:2023-12-02 03:16:18 29 4
gpt4 key购买 nike

我正在开发一个 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.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)

它说没有找到名为“persistence-unit”的 bean。

这是我的 persistence.xml:

<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="persistence-unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>ma.jobcenter.domain.Address</class>
<class>ma.jobcenter.domain.Candidate</class>
<class>ma.jobcenter.domain.RegistredUser</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test-db"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="root"/>
<property name="show_sql" value="false"/>
</properties>
</persistence-unit>

这是我的 AbstractDAO,每个 DAO 类都继承自:

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;
}
}

这是我的 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 上运行。我真的不明白这个异常,也没有找到任何答案。提前致谢。

最佳答案

我找到了解决方案,我必须在 @PersistenceContext 中将单位 Name="persistence-unit"更改为 name="persistence-unit"现在工作正常。

关于java - 未定义名为 'persistence-unit' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40244949/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com