Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:15:02+00:00 2026-06-14T20:15:02+00:00

@Service public class LogProcessorServiceImpl { @Autowired private static ApplicationConfigurationService applicationConfigurationService; public static void processPageRequestsLogs()

  • 0
@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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:15:03+00:00Added an answer on June 14, 2026 at 8:15 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following service object public class UserService { @Autowired private UserDao
I've got the following class: @Service public class TestService{ @Autowired private Integer size; private
this code: @Service public class IvsImport { private Logger logger = Logger.getLogger(IvsImport.class); @Autowired FileReader
@Service @Repository @Transactional public class VideoService { @PersistenceContext EntityManager entityManager; public void save(Video video)
I implemented Service for listening user location: public class ListenLocationService extends Service { private
I defined a local Service: public class ComService extends Service implements IComService { private
Consider a following chunk of service: public class ProductService : IProductService { private IProductRepository
public class ReplyWithAddressService extends Service{ public static final String GOOGLE_GEOCODER = http://maps.googleapis.com/maps/api/geocode/json?latlng=; private String
To paint a scenario, consider the following logic-omitted-Service public class Service { private Validator
I have this service: public class AAAService extends Service { private Intent requestService; private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.