【问题标题】:Entity manager has not been injected实体管理器尚未注入
【发布时间】:2015-07-14 17:27:10
【问题描述】:

我刚开始使用 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;
}

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

【问题讨论】:

  • 您的组件扫描排除了任何包含“Roo”的类,因此 Spring 不会扫描您的 UserDetail_Roo_Jpa_ActiveRecord 类。
  • 感谢您的回复。我尝试删除它,但仍然遇到同样的错误。

标签: java spring jpa spring-roo roo


【解决方案1】:

【讨论】:

  • 感谢您的回复。我没有修改 Roo 生成的文件。我只修改了 applicationContext 文件,我仍然得到同样的错误。
  • 能否附上项目的备份?
【解决方案2】:

我最终找到了问题所在,似乎我的应用程序中没有初始化上下文。 我在 web.xml 中添加了以下行,它起作用了

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

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

【讨论】:

    猜你喜欢
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2010-11-21
    • 1970-01-01
    • 2016-03-24
    • 2012-10-18
    • 2016-01-02
    相关资源
    最近更新 更多