Announcement Announcement Module
Collapse
No announcement yet.
Annotation autowire in ContextListner Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation autowire in ContextListner

    Hello,
    I would like to load some properties in the ServerContext so i addes a Configuration Listener.

    I tried this:

    Code:
    public class ConfigurationListener implements ServletContextListener {
    
    	@Autowired
    	private ApplicationConfigProvider applicationConfigProvider;
    
    	public void contextInitialized(ServletContextEvent event) {
                servletContextEvent.getServletContext().setAttribute("test",applicationConfigProvider.getProperties.getProperty("testKey"));
    	}
    
    	public void contextDestroyed(ServletContextEvent arg0) {
    	}
    }

    But the autowire fails (without error, just the object is null).
    However if i try the old getBean method everything works:

    Code:
    public class ConfigurationListener implements ServletContextListener {
    
    	private ApplicationConfigProvider applicationConfigProvider;
    
    	public void contextInitialized(ServletContextEvent event) {
    
                applicationConfigProvider=(ApplicationConfigProvider) BootstrapFactory.getApplicationContext().getBean("applicationConfigProvider");
                servletContextEvent.getServletContext().setAttribute("test",applicationConfigProvider.getProperties.getProperty("testKey"))
    	}
    
    	public void contextDestroyed(ServletContextEvent arg0) {
    	}
    }
    Can you tell me why?
    Thanks.

  • #2
    Why should it be autowired at all? It isn't a spring bean, it isn't managed by spring but by the servlet container. If you want dependencies you will have tolook them up instead of getting them injected. Make sure your servlet listener comes after the ContextLoaderListener, you can then use the WebApplicationContextUtils to retrieve the bean you need.

    Comment

    Working...
    X