gpt4 book ai didi

java - NoSuchBeanDefinitionException - 未定义名为 blPU 的 bean - 无法注入(inject) EntityManager JPA

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:33 26 4
gpt4 key购买 nike

我有一个看起来像这样的类(class)..

@Repository("myService")
@Transactional
public class MyDaoImpl implements MyDao
{
@PersistenceContext(unitName="blPU")
protected EntityManager em;

@Resource(name="blEntityConfiguration")
protected EntityConfiguration entityConfiguration;

@Override
@Transactional
public Person findPersonByUsername(String username)
{
System.out.println("The entity manager is "+em);
// ....
}

}

以及位于src/main/resources/META-INF/persistence.xml

persistence.xml
<persistence-unit name="blPU" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>jdbc/web</non-jta-data-source>
<class>com.mycompany.dao.MyDaoImpl</class>
<exclude-unlisted-classes/>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.hbm2ddl.import_files" value="/sql/import_storage.sql"/>
</properties>
</persistence-unit>

但是我收到以下错误..

[artifact:mvn] org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'blPU' is defined
[artifact:mvn] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)

jta数据源声明如下..

 <Resource name="jdbc/web"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="30000"
maxActive="15"
maxIdle="10"
minIdle="5"
removeAbandonedTimeout="60"
removeAbandoned="false"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="${database.user}"
password="${database.password}"
driverClassName="${database.driver}"
url="${database.url}"/>

最佳答案

将以下内容添加到 applicationContext.xml 解决了问题。

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<context:annotation-config/>

感谢@geoand的有用评论

关于java - NoSuchBeanDefinitionException - 未定义名为 blPU 的 bean - 无法注入(inject) EntityManager JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24232416/

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