bean的初始化失败;嵌套异常是java.lang.NoClassDefFoundError:org / eclipse / jetty / server / AbstractConnector

时间:2014-04-11 08:21:39

标签: java spring jmx simplejmx

我有Spring项目。我需要使用JMX。我正在使用simplejmx - 我发现它非常有用但不幸的是,我无法管理一些基本的东西。

我写了一个非常简单的服务器:

package com.pckg.jmx;

import com.j256.simplejmx.web.JmxWebServer;

public class JMXServer {

    public static void main(String[] args) throws Exception {

        JmxWebServer jmxWebServer = new JmxWebServer(8123);
        jmxWebServer.start();
    }

}

pom.xml

<project...> 

    <properties>
        <jetty-version>8.1.9.v20130131</jetty-version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.j256.simplejmx</groupId>
            <artifactId>simplejmx</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>${jetty-version}</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>${jetty-version}</version>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

当我运行它时,效果很好。我想以Spring方式做到这一点。我创建了jmx-config文件:

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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- publishes classes that have @JmxResource or implement JmxSelfNaming 
        to jmxServer automagically -->
    <bean id="beanPublisher" class="com.j256.simplejmx.spring.BeanPublisher">
        <property name="jmxServer" ref="jmxServer" />
    </bean>

    <!-- our JmxServer which publishes our beans via JMX -->
    <bean id="jmxServer" class="com.j256.simplejmx.web.JmxWebServer"
        init-method="start" destroy-method="stop">
        <!-- the port should probably come from a configured property -->
        <property name="registryPort" value="8123" />
    </bean>

</beans>

我更改了web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    ...
    jmx-config.xml
    </param-value>
</context-param>

我添加了简单的类:

package com.pckg.jmx;

import com.j256.simplejmx.common.JmxResource;

@JmxResource
public class DummyJMX {

    private int var = 3;
}

当我运行我的应用程序时,我看到了警告:

10:15:07,184 WARN  [org.jboss.as.ee] (MSC service thread 1-1) JBAS011006: Not installing optional component org.eclipse.jetty.continuation.Servlet3Continuation$1 due to exception: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011054: Could not find default constructor for class org.eclipse.jetty.continuation.Servlet3Continuation$1
    at org.jboss.as.ee.component.ComponentDescription$DefaultComponentConfigurator.configure(ComponentDescription.java:606)
    at org.jboss.as.ee.component.deployers.EEModuleConfigurationProcessor.deploy(EEModuleConfigurationProcessor.java:81)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_21]

10:15:07,191 WARN  [org.jboss.as.ee] (MSC service thread 1-1) JBAS011006: Not installing optional component org.eclipse.jetty.continuation.Servlet3Continuation$2 due to exception: org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS011054: Could not find default constructor for class org.eclipse.jetty.continuation.Servlet3Continuation$2
    at org.jboss.as.ee.component.ComponentDescription$DefaultComponentConfigurator.configure(ComponentDescription.java:606)
    at org.jboss.as.ee.component.deployers.EEModuleConfigurationProcessor.deploy(EEModuleConfigurationProcessor.java:81)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_21]

和一个例外(它太长了,我只粘贴了第一部分):

10:16:54,674 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-13) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beanPublisher' defined in class path resource [jmx-config.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/eclipse/jetty/server/AbstractConnector
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) [spring-beans-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) [spring-context-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) [spring-context-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) [spring-web-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) [spring-web-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.6.RELEASE.jar:3.2.6.RELEASE]
    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [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(Unknown Source) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_21]
Caused by: java.lang.NoClassDefFoundError: org/eclipse/jetty/server/AbstractConnector

我一直在寻找解决方案,但我无法解决问题。我的想法是问题出在pom.xml但我不确定,而且,我不知道如何检查它以及我应该改变什么。

提前致谢

2 个答案:

答案 0 :(得分:2)

您将jetty-server依赖关系视为可选,如果您将代码打包为war,则可能(取决于您在pom中具有的其他插件和配置设置)该jar不包含在其中。 检查生成的war文件,并在pom中测试包装中没有<optional>true</optional>的包装,以获得'jetty-server'依赖。

答案 1 :(得分:1)

@Andrei Stefan的回答解决了这个问题,但无论如何,还有另一个问题。

<bean id="jmxServer" class="com.j256.simplejmx.web.JmxWebServer"
    init-method="start" destroy-method="stop">
    <!-- the port should probably come from a configured property -->
    <property name="registryPort" value="8123" />
</bean>

registryPort中没有字段com.j256.simplejmx.web.JmxWebServerClass JmxWebServer。它应该是这样的:

<bean id="jmxServer" class="com.j256.simplejmx.web.JmxWebServer"
    init-method="start" destroy-method="stop">
    <!-- the port should probably come from a configured property -->
    <property name="serverPort" value="8123" />
</bean>

我希望这将有助于未来的发展