Spring boot Spring启动,ServletContextListener.contextInitialized中的自动连线为空

Spring boot Spring启动,ServletContextListener.contextInitialized中的自动连线为空,spring-boot,autowired,servletcontextlistener,Spring Boot,Autowired,Servletcontextlistener,我在contextInitialized中通过自动连接得到了一个NullPointerException,也许有人可以帮助我,谢谢 主课 @SpringBootApplication @CompnentScan public class Application extends SpringBootServletInitializer { public static void main(String[] args) throws Exception { SpringApplicatio

我在contextInitialized中通过自动连接得到了一个NullPointerException,也许有人可以帮助我,谢谢

主课

@SpringBootApplication
@CompnentScan
public class Application extends SpringBootServletInitializer {
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

  @Bean
  public ProfileAdminListener profileAdminListener() {
    return new ProfileAdminListener();
  }
}
ServletContextListener

@WebListener()
public class ProfileAdminListener implements ServletContextListener {

private final Timer timer = new Timer();

public ProfileAdminListener() {
    setProperties();
}

private void setProperties() {
    ....
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    timer.cancel();
    timer.purge();
}

@Override
public void contextInitialized(ServletContextEvent sce) {
    startProtokollTask();
}

private void startProtokollTask() {
    ProtokollFileWriteTask task = ProtokollFileWriteTask.getInstance();
    timer.schedule(task, 0, 10000);
}
}
任务

@组件
公共类ProtokollFileWriteTask扩展了TimerTask{
私有静态ProtokollFileWriteTask实例=新ProtokollFileWriteTask();
@自动连线
专用ProtokollService ProtokollService;
私有ProtokollFileWriteTask(){
}
公共静态ProtokollFileWriteTask getInstance()协议{
返回实例;
}
@凌驾
公开募捐{
writeFile();
}
私有void writeFile(){

protokollService.writeProtokollFile(“c:\temp”);Kryger的anwswer是正确的,这个问题是因为我新建了一个timetask,它不受spring控制。非常感谢。

您可以使用静态方法创建新bean,例如:

@Component
public class ProtokollFileWriterTaskCreator {
  @Autowired
  ProtokollFileWriteTask bean;

  public ProtokollFileWriteTask create() {
    return bean;
  }

}
在另一端,修改您的另一个bean:

@Component
public class ProtokollFileWriteTask extends TimerTask {
  @Autowired
  private ProtokolService protokollService;

  @Override
  public void run() {
    writeFile();
  }

  private void writeFile() {
    protokollService.writeProtokollFile("c:\\temp");
  }

}
在weblistener类中,您应该使用creator bean:

@WebListener
public class ProfileAdminListener implements ServletContextListener {
  private final Timer timer = new Timer();

  @Autowired
  ProtokollFileWriterTaskCreator timerTaskFactory;

  public ProfileAdminListener() {
    setProperties();
  }

  private void setProperties() {
    //nothing
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    timer.cancel();
    timer.purge();
  }

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    startProtokollTask();
  }

  private void startProtokollTask() {
    timer.schedule(timerTaskFactory.create(), 0, 10000);
  }

}
问题是您正在使用操作符new创建bean 这与表示法@Autowire


对不起,我的英语不好

它在springboot页面的示例中起作用了吗???通过一系列调用可能重复您最终使用的
实例
字段,该字段是通过
新建
关键字创建的,因此不是由Spring管理的,因此
@Autowired
注释实际上未使用。您需要立即通过Spring更新所有组件以使自动布线工作。嗨,Kryger,你是对的,这个问题是因为我有一个新的timetask,这不是Spring控制的。非常感谢。
@WebListener
public class ProfileAdminListener implements ServletContextListener {
  private final Timer timer = new Timer();

  @Autowired
  ProtokollFileWriterTaskCreator timerTaskFactory;

  public ProfileAdminListener() {
    setProperties();
  }

  private void setProperties() {
    //nothing
  }

  @Override
  public void contextDestroyed(ServletContextEvent sce) {
    timer.cancel();
    timer.purge();
  }

  @Override
  public void contextInitialized(ServletContextEvent sce) {
    startProtokollTask();
  }

  private void startProtokollTask() {
    timer.schedule(timerTaskFactory.create(), 0, 10000);
  }

}