I have got a SPRING application. When I run
mvn jetty:run
everything is ok.
I would like to use JMX
This is happening as the bean is trying to initialize even after its initialized once.
I was facing the same issue with RabbitMQ configuration in Spring TestNG testcases.
Use @DirtiesContext
as a class level annotation for every testcase class.
This will create applicationcontext and kill it once the testcase class is run and a new applicationcontext will be created for the next testcase class.
Example -
@Test
@ContextConfiguration(classes = { ApplicationConfig.class })
@DirtiesContext
@WebAppConfiguration
public class ATest extends AbstractTestNGSpringContextTests{
@BeforeSuite
public void setup() throws Throwable {
}
@AfterSuite
public void teardown() {
}
}
Caused by: javax.management.InstanceAlreadyExistsException: bean:name=testBean1
This is trying to tell you that you have 2 beans with the same name ObjectName
of bean:name=testBean1
being register with the MBeanExporter
. However, unless there are other XML files or more entries in your beans
map then there should not be.
I have no even idea how to debug it.
You could put a breakpoint in the JmxMBeanServer.registerMBean(...)
method to see what beans are being registered to see if you can figure out why you are getting a duplicate.
As an aside, my SimpleJMX library is an easy way to export your beans via JMX. There is pretty good Spring support as well. Here are the documentation about using with Spring. There is also a Spring test program which demonstrates what you need to do to get it working. Here's the Spring XML file.