javaspringjmxspring-jmx

NoClassDefFoundError: org/springframework/beans/factory/SmartInitializingSingleton


I was recently running into an issue using Spring JMX. The only thing I want to reach is to export a simple Spring Bean for monitoring with JConsole. My goal is to integrate Spring JMX into an existing Spring web application running on an embedded Jetty server. But every time I start the application having JMX configured im running into the following exception:

org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.jmx.export.MBeanExporter] for bean with name 'exporter' defined in URL [file:/C:/Users/max.mustermann/workspace_intranetportal/my_webapp/target/classes/META-INF/appContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/beans/factory/SmartInitializingSingleton

Google couldn't help me so far.

My Spring Bean looks like this:

public class JmxTestBean implements IJmxTestBean {

private String  name;
private int     age;
private boolean isTest;

@Override
public int add(int x, int y) {
    return x + y;
}

@Override
public long myOperation() {
    return 10L;
}

@Override
public int getAge() {
    return age;
}

@Override
public void setAge(int age) {
    this.age = age;
}

@Override
public String getName() {
    return name;
}

@Override
public void setName(String name) {
    this.name = name;
}

And these are the lines I added to Spring's appContext.xml:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
        <map>
            <entry key="test:name=testBean" value-ref="testBean" />
        </map>
    </property>
</bean>

<bean id="testBean" class="com.big.intranet.test.JmxTestBean">
    <property name="name" value="TEST" />
    <property name="age" value="100" />
</bean>

Deleting the lines I just showed you, the application is running fine. Does anyone have an idea how to solve that problem? Thanks everyone!


Solution

  • As I already pointed out in my comment, there're two essential requirements that should be followed when working with Spring JMX:

    1. As M.Deinum explained, always make sure that your Spring dependencies share the same version and that - if possible - always the latest version is used.
    2. Be careful that the bean tag's lazy_init attribute is at least set to false (which should be the default behavior if I remember correctly).

    Following these rules, everything should work well. Thank you all for participating!