实体经理尚未注入

时间:2015-07-14 17:27:10

标签: java spring jpa spring-roo roo

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

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

这是我的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;
}

如果我遗失了某些东西,请告诉我。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我最终弄清了问题,似乎我的应用程序中没有初始化上下文。 我在我的web.xml中添加了以下行,它工作正常

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

如果您的应用程序不是Web应用程序,我假设使用ClassPathXmlApplicationContext初始化上下文应该适合您。