Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Dave Newton has stated in the comments, Spring can only inject beans it manages into other beans it manages. A class annotated with <code>@WebListener</code> is a class managed by the servlet container and so never appears in your Spring context. Even if it does (because you create a <code>bean</code> entry), it won't be the same one the servlet container uses.</p> <p>A way around this is to use Spring's <code>WebApplicationInitializer</code> instead (or alongside) of using a <code>web.xml</code> descriptor. This interface provides an <code>onStartup(ServletContext)</code> method where you can register Servlets, Filters, and Listeners just like you would in a <code>web.xml</code> or with <code>@WebXXX</code>. Something like</p> <pre><code>public void onStartup(ServletContext container) throws ServletException { // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(YourApplicationContext.class); // or some xml file rootContext.refresh(); // Manage the life-cycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's context final AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(DispatcherContext.class); final ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setAsyncSupported(true); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); container.addListener(dispatcherContext.getBean(ExecutorContextListener.class))); ...// more registrations } </code></pre> <p>Therefore the above assumes that your <code>AnnotationConfigWebApplicationContext dispatcherContext</code> contains a bean of type <code>ExecutorContextListener</code>. Because Spring is managing this bean, you can have added <code>@Autowired</code> or <code>@Inject</code> to inject the same <code>AlertServer</code> instance in both this <code>ServletContextListener</code> and a <code>@Controller</code> class. In this scenario you wouldn't initialize it in the <code>contextInitialized()</code> method.</p> <p>A second alternative is to </p> <ol> <li>Add the <code>AlertServer</code> object as an attribute in the <code>ServletContext</code>.</li> <li>Inject the <code>ServletContext</code> into your <code>@Controller</code> class.</li> <li>Retrieve the <code>AlertServer</code> object from the <code>ServletContext</code> attributes.</li> </ol> <p>The above is possible because the <code>contextInitialized()</code> method is always run before any of your controllers start handling requests.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload