sysParamService.getParam("FACILITY_ID");报null异常,sysParamService为空
public class SchedulingRunnable implements Runnable {
@Autowired
private JpaRepository jpaRepository;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private SysParamService sysParamService;
private ReeferTimerSet cron;
public SchedulingRunnable(ReeferTimerSet cron) {
this.cron = cron;
}
@Override
public void run() {
//执行任务
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//定义格式,不显示毫秒
Timestamp now = new Timestamp(System.currentTimeMillis());//获取系统当前时间
String facilityId=sysParamService.getParam("FACILITY_ID");
String str = df.format(now);
String sql = "SELECT unitNo,unitVisitId" +
" FROM YardUnit " +
" WHERE isReefer = '1'";
List list = jpaRepository.findListByJpql(sql, null);
String instertSql = "insert into REEFER_INSPECT_COMMAND (" +
"INSPECT_COMMAND_ID, " +
"TIMER, " +
"INSPECT_STATE, " +
"UNIT_VISIT_ID, " +
"UNIT_NO, " +
"NOTES, " +
"FACILITY_ID," +
"CREATOR, " +
"CREATED_ON, " +
"CHANGER, " +
"CHANGED_ON)" +
"Values(sys_guid(),?,'PLAN',?,?,'',?,'AUTO',?,'AUTO',?)";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
YardUnit yardUnit = list.get(i);
ps.setString(1, str);
ps.setString(2, yardUnit.getUnitVisitId());
ps.setString(3, yardUnit.getUnitNo());
ps.setString(4, facilityId);
ps.setTimestamp(5, now);
ps.setTimestamp(6, now);
}
@Override
public int getBatchSize() {
return list.size();
}
});
System.out.println("***********");
System.out.println("执行完成");
System.out.println("执行完成");
System.out.println("***********");
}
}
原因分析
解决方案
public class SchedulingRunnable implements Runnable {
private JpaRepository jpaRepository;
private JdbcTemplate jdbcTemplate;
private SysParamService sysParamService;
private ReeferTimerSet cron;
public SchedulingRunnable(JpaRepository jpaRepository, JdbcTemplate jdbcTemplate,
SysParamService sysParamService, ReeferTimerSet cron) {
this.jpaRepository = jpaRepository;
this.jdbcTemplate = jdbcTemplate;
this.sysParamService = sysParamService;
this.cron = cron;
}
...
}
参考下面这篇,多线程如何获取ioc容器中的bean
https://www.zhangshengrong.com/p/Ap1Ze27QX0/
需要在你的类上面加@Service注解
你不交给容器管理,怎么注入,没有被容器管理的对象也是拿到bean的
jpa有自己的注解,没有则要进行javaConfig式配置
给你找了一篇非常好的博客,你可以看看是否有帮助,链接:@Autowired注入之后为null你是不是在 test里面启动了线程??
你实现了Runnable,他不受Spring容器管理,需要手动注入bean,测试一些去调Runnable相关代码,看看还是不是null,1.如果不是null,又要Runnable,可以尝试通过应用上下文手动getBean,