1
@Service
public class LogProcessorServiceImpl {
    @Autowired
    private static ApplicationConfigurationService applicationConfigurationService;

    public static void processPageRequestsLogs() {
        if(applicationConfigurationService==null) {
            System.out.println("autowire failed");
        }

I have the ApplicationConfigurationService service autowired like this all over the place and it works fine. The package of this class is being scanned so that's not the problem. It might be related to the way this particular method is called. I have a servlet that is loaded after all other servlets and it fires of a timer that executes the method above with 60 second delay. I assume all autowiring should be completed.

public class ProcessSchedulerServlet implements javax.servlet.Servlet {
    Timer timer=new Timer();

     @Override
     public void init(ServletConfig arg0) throws ServletException {
         timer.scheduleAtFixedRate(new TimerTask() {
             public void run() {
                 LogProcessorServiceImpl.processPageRequestsLogs();
             }
         }, 60*1000, 120*1000);
     }

Here's what happens as soon as I true to use ApplicationConfigurationService:

autowire failed
Exception in thread "Timer-1" java.lang.NullPointerException
at com.siteadmin.services.impl.LogProcessorServiceImpl.processPageRequestsLogs(LogProcessorServiceImpl.java:39)
at com.siteadmin.servlets.ProcessSchedulerServlet$1.run(ProcessSchedulerServlet.java:20)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)

=== 2012-11-18 ============================================================

See also: How to go about Spring autowiring?

Community
  • 1
  • 1
jacekn
  • 1,521
  • 5
  • 29
  • 50

1 Answers1

3

You can't autowire static fields in Spring, this is discussed here

As alternative, if your LogProcessorServiceresides in the root web application context, you can autowire it with Spring WebApplicationContextUtils utility class.

public class ProcessSchedulerServlet implements javax.servlet.Servlet {
     Timer timer=new Timer();

     @Autowired
     LogProcessorService logProcessorService;

     @Override
     public void init(ServletConfig arg0) throws ServletException {
         WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext())
            .getAutowireCapableBeanFactory().autowireBean(this);

         final LogProcessorService svc = this.logProcessorService;
         timer.scheduleAtFixedRate(new TimerTask() {
         public void run() {
             svc.processPageRequestsLogs();
         }
         }, 60*1000, 120*1000);

In general, you should avoid using Java singletons, where using Spring singletons is enough.

Also, if you declared LogProcessorServiceImpl with a @Service annotation, that implies it to be a Spring singleton, so you should not use static fields there at all.

P.S. this answer is about autowiring, it assumes that the idea with TimerTask is correct, in the real apps consider using the Spring Scheduling API

Community
  • 1
  • 1
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • Turns out that I lied and this is the only place where I attempt to autowire a static property. The annotation should detect that it's placed over a static property and report its issue. I wonder how much more time I'll waste on Spring before I start my project over without it. Thanks, Boris. – jacekn Nov 18 '12 at 05:56
  • @jacekn just a little clarification - here we autowire fields of ProcessSchedulerServlet manually because the Servlet is instantiated with the the servlet container and not the Spring app context. Application services are primary candidates to be managed by Spring, so it's reasonable to put them to the Spring container . – Boris Treukhov Nov 18 '12 at 06:48