【问题标题】:How to convert web.xml code to java config for java melody如何将 web.xml 代码转换为 java melody 的 java 配置
【发布时间】:2017-02-06 12:23:03
【问题描述】:

是否有可能从项目中完全消除 web.xml 并将其转换为 Java 配置?

如何将下面的web.xml转换成java配置?

我已经通过几个链接来理解这一点

其中一些是: How to replace web.xml with application context config files?

但是找不到任何教程/博客如何将 web.xml 的每个成员替换为对应的 java 配置..如果有任何可用的东西真的很有帮助..

例如,一些过滤器来自库,我们只需要在 web.xml 中声明功能即可。如何在 java config 中实现相同的功能(用 java config 替换整个 web.xml)

  <filter>
            <filter-name>monitoring</filter-name>
            <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
            <init-param>
                <param-name>allowed-addr-pattern</param-name>
                <param-value>127\.0\..*\..*</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>monitoring</filter-name>
            <url-pattern>/monitoring</url-pattern>
        </filter-mapping>
        <listener>
            <listener-class>net.bull.javamelody.SessionListener</listener-class>
        </listener>


        <login-config>
            <auth-method>BASIC</auth-method>
            <realm-name>Monitoring</realm-name>
        </login-config>

        <security-constraint>
            <web-resource-collection>
                <web-resource-name>Monitoring</web-resource-name>
                <url-pattern>/monitoring</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>jmon-admin</role-name>
            </auth-constraint>
            <!-- if SSL enabled (SSL and certificate must then be configured in the 
                server) <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
                </user-data-constraint> -->
        </security-constraint>

【问题讨论】:

    标签: java spring-security web.xml spring-java-config java-melody


    【解决方案1】:

    我想我终于成功了

    为了监控 sql,将其添加到您的数据源配置中

      @Bean
      public SpringDataSourceBeanPostProcessor monitoringDataSourceBeanPostProcessor() {
        SpringDataSourceBeanPostProcessor processor = new SpringDataSourceBeanPostProcessor();
        processor.setExcludedDatasources(null);
        return processor;
      }
    

    将此添加到您的 securityConfig(您可以针对特定角色对其进行限制)

    .antMatchers("/monitoring/**")
    

    可选步骤是编辑此类(以覆盖 onStartup 方法),但我想默认情况下不需要它

    import net.bull.javamelody.MonitoringFilter;
    import net.bull.javamelody.Parameter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    import javax.servlet.DispatcherType;
    import javax.servlet.FilterRegistration;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import java.util.EnumSet;
    
    public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
      private static final Logger LOGGER = LoggerFactory.getLogger(AppInitializer.class);
      @Override
      protected Class<?>[] getRootConfigClasses() {
        return null;
      }
    
      @Override
      protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{SpringWebConfig.class};
      }
    
      @Override
      protected String[] getServletMappings() {
        return new String[]{"/"};
      }
    
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        FilterRegistration javaMelody = servletContext.getFilterRegistration("javamelody");
        if (javaMelody != null) {
          LOGGER.info("Java Melody Filter Registration found: {}", javaMelody);
          // Filter registered by Servlet Container via web-fragment in
          // javamelody.jar
          addFilter(javaMelody);
        } else {
          LOGGER.info("Java Melody Filter Registration not found. Registering dynamically");
          // Running in embedded server mode.
          FilterRegistration.Dynamic javaMelodyFilter = servletContext.addFilter("javamelody", new MonitoringFilter());
          addFilter(javaMelodyFilter);
        }
      }
    
      private void addFilter(FilterRegistration javaMelody) {
        javaMelody.addMappingForUrlPatterns(
                EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC), true, "/*");
        javaMelody.setInitParameter(Parameter.LOG.getCode(), Boolean.toString(false));
        javaMelody.setInitParameter(Parameter.DISPLAYED_COUNTERS.getCode(), "http,sql,error,log");
      }
    
    }
    

    我找到了 herehere 的来源

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 2019-11-19
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多