2

I get a NullPointerException by Autowiring in contextInitialized, maybe somebody can help me, thanks a lot.

The Main class

@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);
}
}

Task

@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
}
}

After start the appplication, I want to start the timetask, but I get a NullPointerException becuase the protokollservice is null. I think the service was not init before use it.

By use normal spring mvc xml config, this is no problem. How to config it by Spring boot? Thanks.

Abel Pastur
  • 1,905
  • 2
  • 24
  • 31
J.Z.
  • 351
  • 2
  • 15
  • DId it work in example from springboot page ??? – SkorpEN Mar 31 '16 at 11:12
  • Possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – kryger Mar 31 '16 at 11:30
  • 1
    Through a chain of calls you ultimately use the `instance` field that's created via the `new` keyword which is thus not managed by Spring, so the `@Autowired` annotation is effectively unused. You'll need to instantiate all your components via Spring to make autowiring work. – kryger Mar 31 '16 at 11:32
  • Hi Kryger, you are right, this problem is because I **new** a timetask, this is not controlled by spring. Thanks a lot. – J.Z. Mar 31 '16 at 11:46

2 Answers2

0

Kryger's anwswer is right, this problem is because I new a timetask, this is not controlled by spring. Thanks a lot.

J.Z.
  • 351
  • 2
  • 15
0

You colud create the new bean with a static method, by example:

@Component
public class ProtokollFileWriterTaskCreator {
  @Autowired
  ProtokollFileWriteTask bean;

  public ProtokollFileWriteTask create() {
    return bean;
  }

}

By other side, modify your other bean :

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

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

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

}

In weblistener class you should use the 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);
  }

}

The problem was that you were using the operator new to create beans This is incompatible with the notation @Autowire

Sorry for my bad english

Ipman1971
  • 101
  • 1