springboot中使用Schedule注解时@Value以及@Autowired报空指针异常

2023-05-16

文章目录

  • @Value报空指针
  • @Autowired空指针
  • 自定义定时器就不会有注解空指针的问题了
  • 参考文章

@Value报空指针

在使用@value注解获取application.properties文件的内容的时候, 由于使用了@Schedule去创建定时任务, 因为他的加载比较早, 就会导致@value报空指针异常, 可以自己写一个获取配置文件内容的方法, 就可以规避这种错误, 代码如下:
其中: System.getProperty(“user.dir”) + File.separator + “conf” + File.separator + “application.properties”) 获取到的是配置文件的路径, System.getProperty(“user.dir”)获取到的是项目路径, 打成jar包之后代表的就是与该jar包平级的目录conf下的application.properties文件

public class GetPropertiesValue {
    private static Properties properties = null;
    // 初始化
    static {
        properties = new Properties();
        try{
        // resource目录下的配置文件
        // PropertiesUtil.calss.getClassLoader().getResourceAsStream(application.properties)
            InputStream inputStream = new FileInputStream(new File(System.getProperty("user.dir") + File.separator + "conf" + File.separator + "application.properties"));
            properties.load(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Autowired空指针

通过实现ApplicationContextAware来实现注入的操作用以代替Autowtred

public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }
    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }
}

自定义定时器就不会有注解空指针的问题了

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;

@EnableScheduling
@Lazy(value = false)
public class TimeTask implements SchedulingConfigurer {

    private static Logger log = LoggerFactory.getLogger(TimeTask.class);
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                System.out.println("定时器逻辑放这里");
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 每五秒执行一次
                String cron = "0/5 * * * * ?";
                CronTrigger cronTrigger = new CronTrigger(cron);
                Date nextExec = cronTrigger.nextExecutionTime(triggerContext);
                return nextExec;
            }
        });
    }
}

参考文章

获取properties内容
动态配置Schedule
@Autowired空指针异常

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

springboot中使用Schedule注解时@Value以及@Autowired报空指针异常 的相关文章

随机推荐