【问题标题】:Access spring bean in Quartz job在 Quartz 作业中访问 spring bean
【发布时间】:2016-08-19 03:49:39
【问题描述】:

我查看了关于 SO 的大部分帖子,但这些都没有真正帮助。我在使用 spring bean 访问 DAO 时得到了 NPE。

我的调度器

@Repository
@Transactional
public class JobSchedulerImpl implements IJobScheduler {
    Scheduler scheduler;
    Trigger trigger;
    JobDetail job;

    public JobSchedulerImpl() {
    super();
    try {
        scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
    } catch (SchedulerException e) {
        logger.error("Exception while starting the scheduler :{} ", e.getMessage());
    }
}
@Override
public void runWeekly(String whichDay, String userName) throws Exception {
    //whichDay = whichDay.substring(0, 3);
    //String cornJobExpression = "0 07 16 ? * " + whichDay + " *";
    logger.info("Running Weekly Job");
    String cornJobExpression = "0 0/1 * 1/1 * ? *";
    job = JobBuilder.newJob(RunWeeklyJob.class).withDescription("runWeeklyJob_" + userName)
            .withIdentity(userName, "group_runWeekly").build();
    trigger = TriggerBuilder.newTrigger().withIdentity("runWeeklyTrigger_" + userName, "group_runWeekly")
            .withSchedule(CronScheduleBuilder.cronSchedule(cornJobExpression)).startNow().build();
    scheduler.scheduleJob(job, trigger);

     } 
}

职位类别

public class RunWeeklyJob implements Job {

public static final Logger logger = LoggerFactory.getLogger(RunWeeklyJob.class);

@Autowired
private IRunReport  report;

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    //SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    JobDetail jobdetails = context.getJobDetail();
    JobKey jobKey = jobdetails.getKey();
    String name = jobKey.getName();
    report.getReport();
    logger.info("Context : {}", context.toString());

   }
}

报表界面

public interface IRunReport {
public void getReport();
}

实施

@Repository
@Transactional
public class RunReport implements IRunReport{
@Autowired
private IGenericCRUDDao genericDao;
public void getReport()
{
    System.out.println("genericDao" + genericDao);
    User userObj = genericDao.getEntityById(User.class, 1);
    System.out.println("userObj : " + userObj);
}

测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring/applicationContext.xml")
public class RunReportTest {
@Autowired
IJobScheduler jobScheduler;

@Test
public void test_A_AddClients() throws Exception {
    jobScheduler.runWeekly("MONDAY", "Santosh");
    Thread.sleep(70000);
}
}

我在运行计划作业时访问 spring bean genericDao 时获得了 NPE。我尝试了SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 和其他关于 SO 的建议答案,但没有运气。有人可以帮忙解决我哪里出错了。

【问题讨论】:

    标签: java spring spring-mvc cron quartz-scheduler


    【解决方案1】:

    检查是否在您的配置中指定包扫描组件。 例如:

    <context:component-scan base-package="your.package.here" />
    

    Spring 读取它以扫描注释。

    【讨论】:

      【解决方案2】:

      这是 Spring Security 不允许我在 Web 上下文之外注入 bean。一旦我设置了权限,它就会按预期工作。

      有趣的事情没有一个例外可以提供有关安全相关消息的任何线索。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-15
        • 2017-10-16
        • 2011-07-27
        • 1970-01-01
        • 2018-04-23
        相关资源
        最近更新 更多