Entity manager has not been injected

I just started working with Spring ROO and I generated my entity classes using the database reverse engineer command. However whenever I try to call one of the CRUD method in the generated entity class, I keep getting this exception : java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)

I suspect(by looking at the generated files) that the EntityManager was not injected into the class. Please could you tell me what configuration I am missing?

Here is what my applicationContext.xml looks like

    <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>

And the generated entity files

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;
}

Please let me know if there is something I'm missing.


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


I eventually figured out the problem, it seems context was not being initialized in my application. I added the following line in my web.xml and it worked

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

If your application is not a web app, I assume initializing the context using ClassPathXmlApplicationContext should work for you.

链接地址: http://www.djcxy.com/p/25412.html

上一篇: AspectJ + @Configurable

下一篇: 实体经理尚未注入