I had looked into most of the post on SO but none of this really helped. I am getting the NPE while accessing the DAO using spring bean.
My Scheduler
@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);
}
}
Job Class
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());
}
}
Report Interface
public interface IRunReport {
public void getReport();
}
Implementation
@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);
}
test case
@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);
}
}
I am getting the NPE while accessing the spring bean genericDao while running the scheduled job. I tried SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
and other suggested answers on SO but no luck. Can someone help on where I am going wrong.
This was spring security was not allowing me to inject the bean outside the web context. Once I setup the permission it works as expected.
The interesting things that none of exception gives any clue about security related message.