我遇到了一个非常奇怪的错误,我无法理解。 简而言之:我有一个ImporterBean应该读取一个xml文件,然后做一些事情。 ImporterBicker是由ImporterKicker“kickstart”,但是当我启动应用程序时 ImporterBean类中的ApplicationBean和EntityManager为null。他们是 没注入那个豆子。在KickerBean中,正确注入了ImporterBean和ApplicationBean。
请参阅下面的代码,请告诉我我做错了什么(使用seam SEAM 2.2.1.CR2)。
@SuppressWarnings({"UnusedDeclaration"})
@Name("importerBean")
@AutoCreate
public class ImporterBean {
private static final FilenameFilter ONLY_XML_FILES = (FilenameFilter) new SuffixFileFilter(".xml");
public static final String IN_DIR = "IN";
public static final String ERROR_DIR = "ERROR";
public static final String PROCESSED_DIR = "PROCESSED";
@In(create = true)
public ApplicationBean applicationBean;
@In
private EntityManager entityManager;
@Asynchronous
@Transactional
public void runImport(@Duration long firstStart, @IntervalDuration long startTimer) {
log.info("<118100>");
File inDir = Doing some file stuff...
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Inner class bean to kick the background tasks.
*/
@Startup
@Scope(APPLICATION)
@Name("importerKicker")
public static class ImporterKicker {
@In(create = true)
public ImporterBean importerBean;
@In(create = true)
public ApplicationBean applicationBean;
@Create
public void scheduleOptimizer() {
final int interval = applicationBean.getImporter118checkInterval();
if (interval != 0) {
importerBean.runImport(30 * MILLIS_PER_SECOND, interval * MILLIS_PER_SECOND);
} else {
}
}
}
}
答案 0 :(得分:1)
答案 1 :(得分:0)
由于您使用的是异步调用,因此不能在事件范围的组件中使用这样的注入。
而是在Asyncrhonous方法内写:
@Asynchronous
@Transactional
public void runImport(@Duration long firstStart, @IntervalDuration long startTimer) {
EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");
ApplicationBean applicationBean = (ApplicationBean) Component.getInstance("applicationBean",true);
log.info("<118100>");
File inDir = Doing some file stuff...
}
} catch (Exception e) {
e.printStackTrace();
}
}
}