@Autowired注解导入对象实例时出现null的情况

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("***********");


    }
}

原因分析

  1. 从 SchedulingRunnable 这个类上面没有添加 Spring Bean 注解,你这个对象其实是没有纳入 Spring Bean 的管理
  2. 当你试图当你在 SchedulingRunnable 上面添加了 Spring Bean 注解的时候报了 Consider defining a bean of type 'net.huadong.citos.bom.server.scheduled.entity.ReeferTimerSet' in your configuration. 这个错误,这个原因是因为 Spring 在创建 bean 的时候,因为你没有定义无参构建器,只定义了 ReeferTimerSet 这个参数的构建器。而且你并没有定义 ReeferTimerSet 这个类型的 bean 在Spring 当中,所以报了以上报错。
  3. SchedulingRunnable 这个类其实是实现了 Runnable 接口的,对于线程这种消耗资源的操作,我猜测你应该使用了线程池这种技术。
  4. 综上可得,你的 SchedulingRunnable 这个对象并没有纳入 Spring 容器的管理,但是你使用了 Spring 的依赖注入(@Autowired).这本身就不可能。

解决方案

  1. 构建 SchedulingRunnable 这个对象你可以使用构建器方式来传递上面三个使用 Spring @Autowired 来构建对象。如下所示:
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,