gpt4 book ai didi

java - 实体管理器尚未注入(inject)

转载 作者:行者123 更新时间:2023-11-30 08:04:06 25 4
gpt4 key购买 nike

我刚刚开始使用 Spring ROO,并使用数据库逆向工程命令生成了实体类。但是,每当我尝试调用生成的实体类中的 CRUD 方法之一时,我都会收到此异常:java.lang.IllegalStateException:实体管理器尚未注入(inject)(Spring Aspects JAR 是否配置为 AJC/AJDT 方面库?)

我怀疑(通过查看生成的文件)EntityManager 没有注入(inject)到类中。请你告诉我我缺少什么配置?

这是我的 applicationContext.xml 的样子

    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="com.lennartz">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT 1 FROM DUAL"/>
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>

以及生成的实体文件

privileged aspect UserDetail_Roo_Jpa_ActiveRecord {

@PersistenceContext
transient EntityManager UserDetail.entityManager;

public static final EntityManager UserDetail.entityManager() {
EntityManager em = new UserDetail().entityManager;
if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
return em;
}

如果我遗漏了什么,请告诉我。

最佳答案

您不得修改 .aj 文件,要自定义 Roo 生成的代码,请阅读 http://docs.spring.io/spring-roo/docs/2.0.0.M1/reference/html/#edit-modify-and-customize-the-roo-generated-code

关于java - 实体管理器尚未注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413399/

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