Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting a lazily initialized bean (which implements SelfNaming and is annotated with ManagedResource annotation) gives IllegalStateException [SPR-17592] #22124

spring-projects-issues opened this issue Dec 11, 2018 · 2 comments


Copy link

@spring-projects-issues spring-projects-issues commented Dec 11, 2018

Lavish Kothari opened SPR-17592 and commented

I'm having a bean that is

  • annotated with ManagedResource
  • lazily initialized
  • implements SelfNaming

I'm exporting it using spring's AnnotationMBeanExporter.

All this works good when I'm using spring version 4.3.16.RELEASE, but when I upgraded my spring version to 5.0.5.RELEASE or 5.1.3.RELEASE this code started giving me IllegalStateException.

My Bean definition and the spring's context.xml looks like this:

package com.jmx.trial.dummybeans;

import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.jmx.export.naming.SelfNaming;


public class SampleBean implements SelfNaming {
    public ObjectName getObjectName() throws MalformedObjectNameException {
        return new ObjectName("com.jmx.trial:name=sampleBean");



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi=""

    <bean id="server" class=""/>

    <bean id="exporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
        <property name="server" ref="server"/>

    <bean id="sampleBean" class="com.jmx.trial.dummybeans.SampleBean" lazy-init="true"/>


I understand that an extra validation was added here, and this is causing IllegalStateException but I'm not completely sure why that was added.


The stack-trace looks like this:

org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [sampleBean] with key 'sampleBean'; nested exception is java.lang.IllegalStateException: Not initialized

    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(
    at org.springframework.jmx.export.MBeanExporter.lambda$registerBeans$2(
    at java.base/java.util.HashMap.forEach(
    at org.springframework.jmx.export.MBeanExporter.registerBeans(
    at org.springframework.jmx.export.MBeanExporter.afterSingletonsInstantiated(
    at com.jmx.trial.MBeanExporterTest.testBeanExportedWithXml(
    at com.jmx.trial.MBeanExporterTest.testForLazyAutoDetectWithSelfNaming(
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.base/java.lang.reflect.Method.invoke(
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(
    at org.junit.runners.ParentRunner.runLeaf(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    at org.junit.runners.ParentRunner$
    at org.junit.runners.ParentRunner$1.schedule(
    at org.junit.runners.ParentRunner.runChildren(
    at org.junit.runners.ParentRunner.access$000(
    at org.junit.runners.ParentRunner$2.evaluate(
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
    at com.intellij.rt.execution.junit.JUnitStarter.main(
Caused by: java.lang.IllegalStateException: Not initialized
    at org.springframework.util.Assert.state(
    at org.springframework.jmx.export.MBeanExporter$NotificationPublisherAwareLazyTargetSource.postProcessTargetObject(
    at org.springframework.jmx.export.MBeanExporter$NotificationPublisherAwareLazyTargetSource.getTarget(
    at org.springframework.aop.framework.CglibAopProxy$DynamicUnadvisedInterceptor.intercept(
    at com.jmx.trial.dummybeans.SampleBean$$EnhancerBySpringCGLIB$$9cd1c95b.getObjectName(<generated>)
    at org.springframework.jmx.export.MBeanExporter.getObjectName(
    at org.springframework.jmx.export.MBeanExporter.registerLazyInit(
    at org.springframework.jmx.export.MBeanExporter.registerBeanNameOrInstance(
    ... 33 more


My experiments:

  • When I don't lazily initialize the SampleBean, I don't get this IllegalStateException.
  • When I annotate SampleBean with ManagedResource(objectName = "com.jmx.trial:name=sampleBean") and make SampleBean not implement SelfNaming interface, again I don't get IllegalStateException
  • When I debug this and place a lot of breakpoints in MBeanExporter and CGLibAopProxy I don't get this exception. (This is specifically interesting that in debug-mode the tests run successfully but in normal run mode the tests fails.)

Though not very sure, but I think implementing SelfNaming interface is not a very good idea as the javadocs of SelfNaming interface says that:

This interface is mainly intended for internal usage.

I'm not sure if it is the problem with using SelfNaming interface or I'm doing something fundamentally wrong. Can you please explain this behavior and point me to the fundamentals that I'm missing.

PS: some of my findings: (might be unrelated) From here I found this caution point:

Do not use interface-based AOP proxies in combination with autodetection of JMX annotations in your bean classes.

I don't completely understand this, but is this the rule that I'm violating?


I feel that the Assert.state that is getting called in postProcessTargetObject should be moved into the method injectNotificationPublisherIfNecessary and should be called only if managedResource instanceof NotificationPublisherAware is true.

Affects: 5.0.5, 5.1.3

Reference URL:


Issue Links:

  • #20276 Extend null-safety to field level

Referenced from: commits e95c1b3, 2c98c1b

Backported to: 5.0.12

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 12, 2018

Juergen Hoeller commented

This turns out to be a side effect of our nullability efforts in 5.0. I've effectively relaxed it in injectNotificationPublisherIfNecessary itself, simply skipping the callback if there is no state to pass in.

Thanks for raising this! Please give the upcoming 5.1.4.BUILD-SNAPSHOT / 5.0.12.BUILD-SNAPSHOT a try and let me know whether it works for you now...

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Dec 14, 2018

Lavish Kothari commented

Juergen Hoeller Thanks for a quick response.

I confirm that 5.1.4.BUILD-SNAPSHOT works good for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants