Results 1 to 6 of 6

Thread: Autowiring the servlet filter

  1. #1
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default 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. #2
    Join Date
    Jul 2008
    Location
    Columbus, OH
    Posts
    43

    Default

    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
    Thanks,
    Frank Lamantia

  3. #3
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default 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?

  4. #4
    Join Date
    Jul 2008
    Location
    Columbus, OH
    Posts
    43

    Default

    Quote 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
    Thanks,
    Frank Lamantia

  5. #5
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default nevermind

    I figured it out. Thanks for the hints.

  6. #6
    Join Date
    Dec 2007
    Posts
    21

    Default

    You could also have a look at the DelegatingFilterProxy class.

    --
    Regards
    Dries

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •