Spring 引导启动实例化错误

Spring Boot Actuate Instantiation Error

我正在尝试 运行 在 JBoss 容器中启动 Spring 应用程序。我遇到了与此类似的问题:Spring boot war doesn´t work on Jboss 7.1,但他只是从 pom 文件中删除 actuate 的解决方案不起作用,因为我实际上确实需要一些 actuate 功能。

堆栈跟踪;

14:54:33,794 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/rtsweb]] (MSC service thread 1-5) Exception starting filter applicationContextIdFilter: java.lang.InstantiationException: org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration$ApplicationContextHeaderFilter
    at java.lang.Class.newInstance0(Class.java:359) [rt.jar:1.7.0_21]
    at java.lang.Class.newInstance(Class.java:327) [rt.jar:1.7.0_21]
    at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:80) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:72) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:441) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3269) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3865) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_21]

14:54:33,805 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/rtsweb]] (MSC service thread 1-5) Exception starting filter webRequestLoggingFilter: java.lang.InstantiationException: org.springframework.boot.actuate.trace.WebRequestTraceFilter
    at java.lang.Class.newInstance0(Class.java:359) [rt.jar:1.7.0_21]
    at java.lang.Class.newInstance(Class.java:327) [rt.jar:1.7.0_21]
    at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:80) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:72) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:441) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3269) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3865) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_21]

14:54:33,825 ERROR [org.apache.catalina.core.StandardContext] (MSC service thread 1-5) Error filterStart

我不知道如何修复或调试它。我尝试改变一些版本(Spring-boot 作为一个整体并单独启动)并且还尝试使用 spring-boot-starter-actuate 而不仅仅是 spring-boot -启动但没有运气。

我通过将 jboss-deployment-structure.xml 文件添加到我的 webapp 文件夹中的 WEB-INF 来解决这个问题。

<?xml version="1.0" encoding="UTF-8"?>

<jboss-deployment-structure>
    <deployment>
        <exclude-subsystems>
            <subsystem name="jpa" />
        </exclude-subsystems>
        <exclusions>
            <!-- WFCORE-209 workaround -->
            <module name="javaee.api" />
            <module name="javax.persistence.api" />
            <module name="org.hibernate" />
        </exclusions>
        <dependencies>
            <!-- WFCORE-209 workaround -->
            <module name="javax.activation.api" export="true" />
            <module name="javax.annotation.api" export="true" />
            <module name="javax.ejb.api" export="true" />
            <module name="javax.el.api" export="true" />
            <module name="javax.enterprise.api" export="true" />
            <module name="javax.enterprise.deploy.api" export="true" />
            <module name="javax.inject.api" export="true" />
            <module name="javax.interceptor.api" export="true" />
            <module name="javax.jms.api" export="true" />
            <module name="javax.jws.api" export="true" />
            <module name="javax.mail.api" export="true" />
            <module name="javax.management.j2ee.api" export="true" />
            <!-- <module name="javax.persistence.api" export="true"/> -->
            <module name="javax.resource.api" export="true" />
            <module name="javax.rmi.api" export="true" />
            <module name="javax.security.auth.message.api" export="true" />
            <module name="javax.security.jacc.api" export="true" />
            <module name="javax.servlet.api" export="true" />
            <module name="javax.servlet.jsp.api" export="true" />
            <module name="javax.transaction.api" export="true" />
            <module name="javax.validation.api" export="true" />
            <module name="javax.ws.rs.api" export="true" services="export" />
            <module name="javax.xml.bind.api" export="true" />
            <module name="javax.xml.registry.api" export="true" />
            <module name="javax.xml.soap.api" export="true" />
            <module name="javax.xml.ws.api" export="true" />

            <!-- This one always goes last. -->
            <module name="javax.api" export="true" />
        </dependencies>
    </deployment>
</jboss-deployment-structure>

已采取全部步骤; 编辑 pom 文件以构建为 war 而不是 jar;

<groupId>project</groupId>
<artifactId>project-web</artifactId>
<version>0.4.0-SNAPSHOT</version>
<packaging>war</packaging>

禁止嵌入容器;

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

在 WebConfig 文件中为调度程序 servlet 添加一个 init 方法;

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        ...
        initDispatcherServlet(servletContext, disps);
        ...
    } 


private void initDispatcherServlet(ServletContext servletContext, EnumSet<DispatcherType> disps) {

        // Using listener to be able to get the Dispatcher Servlet not yet initialized
        servletContext.addListener(new ServletContextListener() {

            @Override
            public void contextInitialized(ServletContextEvent event) {
                try {
                    Map<String, ? extends ServletRegistration> servlets=null;
                    servlets = event.getServletContext().getServletRegistrations();
                    Set<String> keys = servlets.keySet();       

                    log.debug("Registred servlets : "+keys);

                    ServletRegistration dspSrvlt = servlets.get("dispatcherServlet");
                    if(dspSrvlt != null) {
                        Collection<String> maps = dspSrvlt.getMappings();
                        log.debug("Dispatcher servlet mapping size : "+maps.toArray().length);
                        log.debug("Servlet dispatcher mapping : "+maps);
                        if( !maps.contains("/*") ) {
                            log.debug("Adding /* for Spring Dispatcher servlet");
                            servlets.get("dispatcherServlet").addMapping("/*");
                        }
                    } else {
                        log.warn("Unable to change the Servlet Request dispatcher mapping to allow deployment with JBoss");
                    }
                } catch (Exception e) {
                    log.warn("Unable to change the Servlet Context to allow deployment with JBoss");
                }

            }

            @Override
            public void contextDestroyed(ServletContextEvent arg0) {
            }
        });

    }

这些是我将 JHipster 生成的应用程序部署到 JBoss 7.1/EAP 6.3 时采取的所有步骤。

希望它能有所帮助,因为它真的很痛苦。