0

我已经使用 cron4j 构建了一个 Web 应用程序和计划的 cron 作业。在执行 cron 时,run() 方法正在调用,并且在 run() 方法中,所有其他 bean 对象都显示为 null。因此,我得到 NullPointerException。下面是我的示例代码。

类员工{

@autowired IEmployeeService 员工服务;

公共无效运行(){

员工服务.getEmployeeDetails();

} }

上面的示例employeeService 对象获取null 和getEmployeeDetails() 中的所有其他bean 对象;正在获取 null 并且 getJdbcTemplate() 也为 null。

使用 cron4j 执行 cron 时如何在 spring 中初始化 bean 对象。

4

1 回答 1

0

您可以使用 @BeanFactoryPostProcessor 注释来命令创建 bean 并在 bean 初始化结束时创建/运行您的作业。

The definition of BeanFactoryPostProcessor to bean (configuration metadata) processing. That is to say, the Spring IoC container allows configuration of BeanFactoryPostProcessor metadata in the container before the actual read instantiate any other bean, and may modify it. If you want, you can configure multiple BeanFactoryPostProcessor. You can control the BeanFactoryPostProcessor by setting the'order'attribute of the execution order.

来自 Spring 文档:

The next extension point that we will look at is the org.springframework.beans.factory.config.BeanFactoryPostProcessor. The semantics of this interface are similar to those of the BeanPostProcessor, with one major difference: BeanFactoryPostProcessor operates on the bean configuration metadata; that is, the Spring IoC container allows a BeanFactoryPostProcessor to read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessors.

请查找有关 Spring 文档的更多信息:http: //docs.spring.io/spring/docs/4.1.3.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#beans-factory-extension-factory-postprocessors

于 2014-12-10T15:02:05.537 回答