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 8527313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:31:22+00:00 2026-06-11T08:31:22+00:00

I have a custom ServletContextListener that I am using to initialize and start up

  • 0

I have a custom ServletContextListener that I am using to initialize and start up a Cron4J scheduler.

public class MainListener implements ServletContextListener {
    @Value("${cron.pattern}")
    private String dealHandlerPattern;

    @Autowired
    private DealMoqHandler dealMoqHandler;
}

I am autowiring some objects in the Listener as shown, and would like for Spring to manage the listener’s instantiation. I am using programmatic web.xml configuration through WebApplicationInitializer, but so far the Listener isn’t being autowired (NullPointerExceptions whenever I try to access the supposedly autowired objects).

I’ve already tried to add my customer listener after adding the ContextLoaderListener, shown below:

public class CouponsWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(SpringAppConfig.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        container.addListener(new MainListener()); //TODO Not working
    }

I checked these past questions Spring – Injecting a dependency into a ServletContextListener and dependency inject servlet listener and tried to implement the following code inside the contextInitialized method of my listener:

    WebApplicationContextUtils
        .getRequiredWebApplicationContext(sce.getServletContext())
        .getAutowireCapableBeanFactory()
        .autowireBean(this);

However, I just get the following exception:

Exception sending context initialized event to listener instance of class com.enovax.coupons.spring.CouponsMainListener: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]

How can I make sure that Spring has already finished instantiation before adding my listener?

  • 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-11T08:31:24+00:00Added an answer on June 11, 2026 at 8:31 am

    My mistake. It turns out the code in the original post is correct, and the listeners are being added in the correct order.

    The issue was that I had annotated my custom listener with a @WebListener annotation (Servlet 3.0). This was causing the web app to disregard my addListener() code in WebApplicationInitializer, and instantiate the custom listener AHEAD of Spring’s ContextLoaderListener.

    The following code block illustrates the ERRONEOUS code:

    @WebListener /* should not have been added */
    public class CouponsMainListener implements ServletContextListener {
        @Autowired
        private Prop someProp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Initializer class that implements the ServletContextListener interface. In its contextInitialized() method,
I have custom exceptions in my django project that look like this: class CustomFooError(Exception):
I have custom class that implement IEnumerable<int[]> interface and save current Enumerator . When
I have custom class that extends WebViewPage that I use as the base for
I have custom control - using Win Forms, that contains four TextBoxes, all have
I have custom ImageView: public class ShadowedImageView extends ImageView { private Paint mPaint; public
I have custom Application class in my Android project that is properly described in
I have custom ContentControl called TabItem public class TabItem : ContentControl I have set
I have Custom List to display image with it's name like below... public class
I have custom app class public class MyApp extends Application { public static Context

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.