Announcement Announcement Module
Collapse
No announcement yet.
Autowiring the servlet filter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring the servlet filter

    I am trying to autowire a bunch of dao objects in my custom servlet filter and for some reason, autowiring does work with controllers but does not work in my servlet filter. Here is my servlet filter:
    Code:
    @Component("appFilter")
    public class MyAppFilter implements Filter
    {
    
        
        @Autowired
        @Qualifier("userDao")
        private UserDao userDao;
        
        public void destroy()
        {
            // TODO Auto-generated method stub
            
        }
    
        public void doFilter( ServletRequest arg0, ServletResponse arg1, FilterChain arg2 ) throws IOException, ServletException
        {
           System.out.println("FILTER>>>>>>>>>>>>>>"+userDao.find( "from User" ));
            
        }
    
        public void init( FilterConfig arg0 ) throws ServletException
        {
            // TODO Auto-generated method stub
            
        }
        
    }
    and it throws a null pointer exception. Is there something special I have to do to autowire a filter versus a controller class?

  • #2
    Your controllers will be created and managed by Spring, so they are candidates to be autowired. Filter's are created and managed by your servlet container, so they are instantiated outside of Spring's container.

    Spring can 'autowire' your Filters and Servlet's, but you have to make Spring "aware" of their existence.

    The cleanest approach to this is using a ContextLoaderListener configured in your web.xml like so:

    Code:
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           classpath:/applicationContext1.xml
        </param-value>
       </context-param>
    
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    This will load your ApplicationContext when the servlet context is created. It will also "publish" this ApplicationContext instance to your 'ServletContext'.

    In your Filter's init() method, you can now use WebApplicationContextUtils to retrieve an instance of that ApplicationInstance (from the ServletContext) like so:

    Code:
    	public void init(FilterConfig filterConfig) throws ServletException {
    
    		ServletContext servletContext = filterConfig.getServletContext();
    		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    		
    		AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();
    		
    		autowireCapableBeanFactory.configureBean(this, BEAN_NAME);
    	}
    For your reference:

    http://static.springframework.org/sp...textUtils.html
    http://static.springframework.org/sp...rListener.html
    http://static.springframework.org/sp...anFactory.html

    Hope that helps

    Comment


    • #3
      What is bean name

      Code:
        autowireCapableBeanFactory.configureBean(this, BEAN_NAME);
      What is the BEAN_NAME variable? Is that the name in the qualifier of my autowired dao object?

      Comment


      • #4
        Originally posted by solid View Post
        Code:
          autowireCapableBeanFactory.configureBean(this, BEAN_NAME);
        What is the BEAN_NAME variable? Is that the name in the qualifier of my autowired dao object?
        Yes Sorry

        Comment


        • #5
          nevermind

          I figured it out. Thanks for the hints.

          Comment


          • #6
            You could also have a look at the DelegatingFilterProxy class.

            --
            Regards
            Dries

            Comment

            Working...
            X