我已经调查了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);
和其他建议的答案,但没有运气。有人可以帮我解决我的错误。
答案 0 :(得分:0)
检查是否在配置中指定了包扫描组件。 例如:
<context:component-scan base-package="your.package.here" />
Spring读取它以扫描注释。
答案 1 :(得分:0)
这是Spring安全性不允许我在Web上下文之外注入bean。一旦我设置了权限,它就会按预期工作。
有趣的事情是,没有任何例外可以提供有关安全相关信息的任何线索。