1

我正在尝试在应用程序启动时将一些数据加载到我的数据库中。我为此目的定义了一个bean

应用程序上下文.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

4

2 回答 2

0

我使用弹簧 SmartLifecycle:http: //static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/SmartLifecycle.html

我创建了一个可以很好地做到这一点的类,只需扩展和覆盖 run 方法:

public abstract class StartupService implements SmartLifecycle {
/** Logger for this class and subclasses */
protected static final Logger logger = LoggerFactory.getLogger(StartupService.class);

protected boolean running=false;

////////////////////// Lifecycle Methods \\\\\\\\\\\\\\\\\\
@Override
public void start() {
    try {
        logger.info("starting {}", this.getClass().getSimpleName());
        run();
    } catch (Exception e) {
        logger.error("failed to run importer", e);
    }
}

@Override
public void stop() {
    running=false;
}

@Override
public boolean isRunning() {
    return running;
}

////////////////////// SmartLifecycle Methods \\\\\\\\\\\\\\\\\\
@Override
public boolean isAutoStartup() {
    return true;
}

@Override
public void stop(Runnable callback) {
    stop();
    callback.run();
}

@Override
public int getPhase() {
    return 0;
}

public abstract void run() throws Exception;
}
于 2012-11-15T21:34:42.133 回答
0

我过去通过将组件扫描定义移动到应用程序上下文的末尾解决了这个问题,如此处所建议

于 2012-11-08T22:48:20.213 回答