2014. 3. 14. 04:46

Servlet Filter에서 Spring Bean 사용 Java/Servlet2014. 3. 14. 04:46

Filter에서 Spring Bean을 사용할거라면, 그냥 Handler Interceptor를 쓰면 되는거 아니냐고 할지 모르겠지만, 하다보니 이렇게 할수 밖에 없는 경우가 생기기도 하더라...


Spring과는 별개로 동작하는 Servlet 기반의 상용 솔루션이 같은 Web Application에 존재하고, 세션체크 등 어려가지 상황은 같이 컨트롤해야하는 경우가 대표적... 




1. web.xml 설정


1
2
3
4
5
6
7
8
9
    <filter>
        <filter-name>testFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>testFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>




2. Filter Class설정

 - 반드시 bean id와 filter-name이 일치해야 한다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Component("testFilter")
public class TestFilter implements Filter {
    
 
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    protected FilterConfig    config    = null;
 
    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }
 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
            
            // TODO .......
 
            chain.doFilter(request, response);
    }
 
    
    @Override
    public void destroy() {
        this.config = null;
    }
}
 




모든 설정이 완료되었는데도 만약 아래와 같은 에러가 발생하면...

1
2
3
4
5
6
7
8
9
10
심각: Exception starting filter testFilter
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testFilter' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236)
...............


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/root-context.xml
            /WEB-INF/config/servlet-context.xml  <!--이부분 확인-->
        </param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/config/servlet-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


보통 DispatcherServlet쪽에만 스프링 context 파일 설정을 하는 경우도 있는데...

contextConfigLocation에 스프링 context 파일이 추가되어있는지 체크한다.


:
Posted by 정규식