1

我刚开始使用 Spring ROO,并使用数据库逆向工程命令生成了我的实体类。但是,每当我尝试在生成的实体类中调用其中一个 CRUD 方法时,我都会收到此异常: java.lang.IllegalStateException: Entity manager has not been injection(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;
}

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

4

2 回答 2

1

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

于 2015-07-15T11:19:36.133 回答
0

我最终发现了问题,似乎我的应用程序中没有初始化上下文。我在 web.xml 中添加了以下行并且它有效

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

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

于 2015-07-19T16:20:10.810 回答