Spring引导,Autowired在ServletContextListener.contextInitialized

时间:2016-03-31 11:04:25

标签: spring-boot autowired servletcontextlistener

我在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);
}
}

任务

@Component
public class ProtokollFileWriteTask extends TimerTask {

private static ProtokollFileWriteTask instance = new ProtokollFileWriteTask();

@Autowired
private ProtokollService protokollService;

private ProtokollFileWriteTask() {
}

public static ProtokollFileWriteTask getInstance() {
    return instance;
}

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

private void writeFile() {
    protokollService.writeProtokollFile("c:\temp");  <---- prtokollService is null
}
}

启动应用程序后,我想启动时间任务,但是我得到一个NullPointerException,因为protokollservice为null。我认为在使用它之前服务不是init。

通过使用普通的spring mvc xml配置,这没问题。如何通过Spring启动配置它?感谢。

2 个答案:

答案 0 :(得分:0)

Kryger的anwswer是对的,这个问题是因为我新增了一个时间任务,这不受春天的控制。非常感谢。

答案 1 :(得分:0)

colud用静态方法创建新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

符号不符

抱歉我的英文不好