【问题标题】:spring cron4j scheduling method invoked before bean initialization在bean初始化之前调用的spring cron4j调度方法
【发布时间】:2014-11-07 05:06:20
【问题描述】:

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

类员工{

@autowired IEmployeeService 员工服务;

公共无效运行(){

employeeService.getEmployeeDetails();

} }

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

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

【问题讨论】:

    标签: spring scheduling cron4j


    【解决方案1】:

    您可以使用 @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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多