我正在使用 Deltaspike (1.4.0) 和 Quartz (2.2.1) 来安排工作。在TestJob bean 中注入了ResourceBundle。ResourceBundle 的生产者需要一个 facesContext,但这始终为 null。
如何在调度程序 bean 中正确注入 ResourceBundle 以及为什么使用 @Scheduled 时 facesContext 始终为空?
@Scheduled(cronExpression = "0 0/1 * * * ?")
public class TestJob implements Job {
@Inject private EntityManager em;
@Inject private transient ResourceBundle i18n;
public TestJob() {}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("TestJob executed...");
}
}
资源包生产者
public class ResourceBundleProducer implements Serializable {
@Inject public FacesContext facesContext;
@Produces
public ResourceBundle getResourceBundle() {
if (facesContext.getViewRoot() != null)
return ResourceBundle.getBundle("i18n.i18n", facesContext.getViewRoot().getLocale());
else
return ResourceBundle.getBundle("i18n.i18n", facesContext.getApplication().getViewHandler().calculateLocale(facesContext));
}
}
FacesContext 生产者
public class FacesContextProducer implements Serializable {
@Produces
@RequestScoped
public FacesContext produceFacesContext() {
return FacesContext.getCurrentInstance();
}
}