设置初始化参数
对应了web.xml 的context-param
@Service
public class ServletInitialize implements ServletContextInitializer {
private String appkey = "12011554";//一个appkey就填一个,多个appkey使用英文逗号分割.
private String name = "fff";
@Override
public void onStartup(ServletContext servletContext) {
Assert.notNull(appkey, "appkey 不能为空");
servletContext.setInitParameter("secret_app_key", name);
servletContext.setInitParameter("groupdataid", appkey);
servletContext.setInitParameter("call_back_class_name", SecretCallback.class.getName());
}
}
设置Listener和Filter
@Configuration
public class WebConfig {
@Bean
public ServletListenerRegistrationBean<ZkclientListener> servletListenerRegistrationBean() {
ServletListenerRegistrationBean<ZkclientListener> bean = new ServletListenerRegistrationBean<>(new ZkclientListener());
bean.setEnabled(true);
return bean;
}
//这是设置filter的一种方式
@Bean
public FilterRegistrationBean<LogbackContextFilter> logbackContextFilter() {
FilterRegistrationBean<LogbackContextFilter> bean = new FilterRegistrationBean<>(new LogbackContextFilter());
bean.setEnabled(true);
bean.setUrlPatterns(Collections.singleton("/*"));
return bean;
}
}
设置filter的第二种方式
//使用ServletComponentScan注解扫描 @WebListener/@WebFilter等等
@ServletComponentScan("com.raycloud.operation.config")
@WebListener
@WebFilter(value = "/*", initParams = {
@WebInitParam(name = "ignore", value = "/aa/**",
description = "过滤路径描述")
})
public class SessionFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
//init
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//biz
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}