2012-10-31 100 views
1

我有在web.xml中有以下一個Spring MVC Web應用程序:的Java EE條件過濾器映射

<filter> 
    <filter-name>foo</filter-name> 
    <filter-class>bar.foo</filter-class> 
    <init-param> 
     <param-name>yada</param-name> 
     <param-value>baz</param-value> 
     <description>fooz.</description> 
    </init-param> 
</filter> 

<filter-mapping> 
    <filter-name>foo</filter-name> 
    <url-pattern>*.htm</url-pattern> 
</filter-mapping> 

這是在生產環境中運行完全正常,但不能同時我發展我的本地機器。

有什麼方法可以設置Maven屬性是否已啓用或禁用,具體取決於活動的構建配置文件?

回答

3

只需執行過濾器代碼本身的檢查。

private boolean disabled; 

@Override 
public void init(FilterConfig config) throws ServletException { 
    disabled = lookupSomeEnvironmentVariableAndReturnBooleanAccordingly(); 
} 

@Override 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
    if (disabled) { 
     chain.doFilter(request, response); 
     return; 
    } 

    // Original code here. 
    // ... 
} 

Maven is able to set environment variables per profile