设置初始化参数

对应了web.xml 的context-param

  1. @Service
  2. public class ServletInitialize implements ServletContextInitializer {
  3. private String appkey = "12011554";//一个appkey就填一个,多个appkey使用英文逗号分割.
  4. private String name = "fff";
  5. @Override
  6. public void onStartup(ServletContext servletContext) {
  7. Assert.notNull(appkey, "appkey 不能为空");
  8. servletContext.setInitParameter("secret_app_key", name);
  9. servletContext.setInitParameter("groupdataid", appkey);
  10. servletContext.setInitParameter("call_back_class_name", SecretCallback.class.getName());
  11. }
  12. }

设置Listener和Filter

  1. @Configuration
  2. public class WebConfig {
  3. @Bean
  4. public ServletListenerRegistrationBean<ZkclientListener> servletListenerRegistrationBean() {
  5. ServletListenerRegistrationBean<ZkclientListener> bean = new ServletListenerRegistrationBean<>(new ZkclientListener());
  6. bean.setEnabled(true);
  7. return bean;
  8. }
  9. //这是设置filter的一种方式
  10. @Bean
  11. public FilterRegistrationBean<LogbackContextFilter> logbackContextFilter() {
  12. FilterRegistrationBean<LogbackContextFilter> bean = new FilterRegistrationBean<>(new LogbackContextFilter());
  13. bean.setEnabled(true);
  14. bean.setUrlPatterns(Collections.singleton("/*"));
  15. return bean;
  16. }
  17. }

设置filter的第二种方式

  1. //使用ServletComponentScan注解扫描 @WebListener/@WebFilter等等
  2. @ServletComponentScan("com.raycloud.operation.config")
  1. @WebListener
  2. @WebFilter(value = "/*", initParams = {
  3. @WebInitParam(name = "ignore", value = "/aa/**",
  4. description = "过滤路径描述")
  5. })
  6. public class SessionFilter implements Filter {
  7. @Override
  8. public void init(FilterConfig filterConfig) {
  9. //init
  10. }
  11. @Override
  12. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  13. //biz
  14. chain.doFilter(request, response);
  15. }
  16. @Override
  17. public void destroy() {
  18. }
  19. }