我已经实现了如下过滤器 -
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
@WebFilter("/login.jsf")
public class ServiceFilter implements ContainerRequestFilter, ContainerResponseFilter, Filter {
public ServiceFilter() {}
@PostConstruct
public void init() {
logger.debug("initialized.");
}
@Override
public void destroy() {
logger.debug("destroyed");
}
@Override
public void init(FilterConfig arg0) throws ServletException {
//nothing here
}
...other method overides
}
当我在Wildfly10
上部署它时,我看到下面的内容被打印
TIMESTAMP DEBUG ServiceFilter:65 - Initialized (ServiceFilter@39df1f0b)
TIMESTAMP DEBUG ServiceFilter:65 - Initialized (ServiceFilter@1a33dd9)
ServiceFilter
类也在实现javax.ws.rs.core.Application
的类之一中注册为根资源。此类充当休息和Web资源的入口点。
如何避免ServiceFilter
类被初始化两次?或者,这是否可以,因为过滤器实现了servlet和rest过滤器?或者,我应该在他们自己的类文件中移动实现吗?