实体经理尚未注入
我刚开始使用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;
}
请让我知道是否有什么我失踪。
您不得修改.aj文件,以自定义Roo生成的代码,请阅读http://docs.spring.io/spring-roo/docs/2.0.0.M1/reference/html/#edit-modify-and-customize -the-袋鼠生成码
我最终发现了这个问题,似乎上下文并没有在我的应用程序中初始化。 我在我的web.xml中添加了以下行,它工作
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
如果您的应用程序不是一个Web应用程序,我假设使用ClassPathXmlApplicationContext来初始化上下文应该适合您。
链接地址: http://www.djcxy.com/p/25411.html上一篇: Entity manager has not been injected
下一篇: Entity manager has not been injected Spring Aspect Jar on remote host OpenShift