【发布时间】:2012-11-08 05:32:16
【问题描述】:
我正在尝试在应用程序启动时将一些数据加载到我的数据库中。我为此目的定义了一个 bean
applicationContext.xml
<bean class="com.project.loader.DataLoader"
id="DataLoader"
depends-on="entityManagerFactory"
scope="singleton"/>
类:
@RooJavaBean
@RooConfigurable
public class DataLoader implements InitializingBean
它正在执行,但是在调用第一个 persist() 方法时,Spring 向我抛出以下错误:
Caused by: java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
at com.project.lib.model.extensions.i18n.Locale_Roo_Jpa_ActiveRecord.entityManager_aroundBody0(Locale_Roo_Jpa_ActiveRecord.aj:19)
at com.project.lib.model.extensions.i18n.Locale_Roo_Jpa_ActiveRecord.ajc$interMethod$com_project_lib_model_extensions_i18n_Locale_Roo_Jpa_ActiveRecord$com_project_lib_model_extensions_i18n_Locale$entityManager(Locale_Roo_Jpa_ActiveRecord.aj:1)
at com.project.lib.model.extensions.i18n.Locale.entityManager(Locale.java:1)
以前当我以下列方式定义 DataLoader 时它正在工作:
@Component
@Configurable
public class DataLoader implements ApplicationListener
但使用原始类型不是一个好习惯,所以我想切换
我怎样才能让它工作?
春季版:3.1
【问题讨论】:
-
你认为删除@Component 重要吗?
-
在我当前的实现(最上面一个)中,组件被移除,bean 被定义在 applicationContext 中。它正在使用@Component 和 IoC 扫描,但是问题在于使用原始类型 ApplicationListener 接口的不良做法。我认为 Bean 方法被调用得太早了......
-
如果您只担心原始类型,为什么不直接使用
ApplicationListener<ContextRefreshedEvent>或类似的东西? -
...最重要的是我想要一些我可以控制执行顺序的东西。对其他 bean 的依赖对我有用,但是在这种情况下,对 entityManagerFactory 的依赖是不够的。
标签: java spring web-applications spring-roo