简体   繁体   English

如何将 web.xml 代码转换为 java melody 的 java 配置

[英]How to convert web.xml code to java config for java melody

Is it possible to completely eliminate web.xml from a project and convert it into Java configuration?是否可以从项目中完全消除 web.xml 并将其转换为 Java 配置?

How to convert the following web.xml to java configuration?如何将以下 web.xml 转换为 java 配置?

I have gone through few links for understanding this我已经通过几个链接来理解这一点

Some of them are : How to replace web.xml with application context config files?其中一些是: 如何用应用程序上下文配置文件替换 web.xml?

But could not find any tutorial/blog how to replace each member of web.xml to correspnding java config..It would be really helpful if anything is available..但是找不到任何教程/博客如何将 web.xml 的每个成员替换为对应的 java 配置..如果有任何可用的东西会非常有帮助..

For example some of the filters come via libraries and we just need to declare in web.xml for functionality..How can the same be achieved in java config (replacing entire web.xml with java config)例如,一些过滤器来自库,我们只需要在 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>

I guess I managed it finally我想我终于成功了

For monitoring sql add this to your datasources config为了监控 sql,将此添加到您的数据源配置中

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

add this to your securityConfig (you can restrict it for specific roles)将此添加到您的 securityConfig (您可以将其限制为特定角色)

.antMatchers("/monitoring/**")

and optional step is edit this class (in order to override onStartup method) but I guess it's not needed by default可选步骤是编辑此类(以覆盖 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");
  }

}

I found this sources here and here我在这里这里找到了这个来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Melody web.xml过滤器不适用于Jetty Server - Java Melody web.xml filters not working with Jetty Server 在Spring Boot中将web.xml转换为Java配置 - Convert web.xml to java config in Spring Boot 我如何将web.xml中的login-config转换为Java类? - How i convert login-config in web.xml to java class? Servlet-web.xml与Java配置 - Servlet - web.xml vs Java config Spring 中的 404 错误(java 配置/无 web.xml) - 404 error in Spring (java config / no web.xml) Web.xml和Java Config的组合未定义名为“ springSecurityFilterChain”的bean - No bean named 'springSecurityFilterChain' is defined in combination of Web.xml and Java Config 用java配置完全替换web.xml(Spring MVC 4 - Tomcat 7/8) - Completely replace web.xml with java config (Spring MVC 4 - Tomcat 7/8) Spring 3.2 Servlet 3.0 Java Config(无web.xml)如何创建自定义404处理程序 - Spring 3.2 servlet 3.0 Java Config (no web.xml) how to create custom 404 handler 如何将基于 Java 的 Spring 配置添加到现有的 web.Z0F635D0E0F3874FFF8B78E 中? - How do you add Java Based Spring config to an existing web.xml? 如何同时为Spring Security正确定义web.xml和基于Java的配置 - How to correctly define web.xml and java-based config for Spring Security at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM