depends-on attribute when using Spring with JMX and a Spring-provided RMI Connector.The next code snippet is an example of a basic Spring configuration file. Most of it is unimportant for this example, but the highlighted section shows what is really important for this blog entry.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="jmxService"
class="marx.SimpleBean">
<description>A JMX Service</description>
<property name="action" value="Spring Action">
<description>Spring Action is described here.</description>
</property>
<property name="behavior" value="Spring Behavior" />
</bean>
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="dustin:type=marx"
value-ref="jmxService" />
</map>
</property>
<property name="assembler" ref="assembler" />
</bean>
<bean id="assembler"
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<list>
<value>marx.SimpleBeanIf</value>
</list>
</property>
</bean>
<bean id="serverConnector"
class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName" value="connector:name=rmi"/>
<property name="serviceUrl"
value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi"/>
</bean>
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="1099"/>
</bean>
</beans>
I am not showing the actual bean class exposed here or the interface it implements because these details are not important for the purposes of this blog entry. When the full application is run with the XML as configured above, the following stack trace is encountered.

The key text of this exception stack trace is:
"Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverConnector' defined in file [C:\NetBeansProjects\SpringJmxExample\SpringConfiguration.xml]: Invocation of init method failed; nested exception is java.io.IOException: Cannot bind to URL [rmi://localhost:1099/jmxrmi]: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused: connect]."
There are a couple ways to make the error go away. One way is to remove the highlighted XML above and specify the RMI registry information on the command line using the following three flags:
- -Dcom.sun.management.jmxremote.port=1099
- -Dcom.sun.management.jmxremote.authenticate=false
- -Dcom.sun.management.jmxremote.ssl=false
When the Spring-based application is run with the above three system parameters, the application works properly. However, this requires specifying the three parameters above on the command-line.
To keep the RMI registry information in the Spring XML configuration, the RMI registry entry either needs to be placed in the XML configuration file above the ConnectorServerFactoryBean entry or the serverConnector (ConnectorServerFactoryBean) element needs to reference the RMI registry. The second option, having the serverConnector reference the RMI registry is accomplished using the
depends-on attribute. The modified code listing below shows the rearrangement of the RMI Registry entry to precede the serverConnector and it also shows use of the depends-on attribute. Either of these changes will make the example work without any special command-line system parameters, but I like to do both.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="jmxService"
class="marx.SimpleBean">
<description>A JMX Service</description>
<property name="action" value="Spring Action">
<description>Spring Action is described here.</description>
</property>
<property name="behavior" value="Spring Behavior" />
</bean>
<bean class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="dustin:type=marx"
value-ref="jmxService" />
</map>
</property>
<property name="assembler" ref="assembler" />
</bean>
<bean id="assembler"
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<list>
<value>marx.SimpleBeanIf</value>
</list>
</property>
</bean>
<!-- Placed RMI registry before serverConnector. -->
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="1099"/>
</bean>
<bean id="serverConnector"
class="org.springframework.jmx.support.ConnectorServerFactoryBean"
depends-on="registry">
<property name="objectName" value="connector:name=rmi"/>
<property name="serviceUrl"
value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi"/>
</bean>
</beans>
As stated above, either action (placing the RMI registry in the Spring XML configuration before the serverConnector or using the depends-on attribute) will make the example work. The advantage of the
depends-on attribute is that the XML file can be changed more safely without order of elements causing trouble.Neither the use of the
depends-on attribute or the ordering of the serverConnector (ConnectorServerFactoryBean) and registry (RmiRegistryFactoryBean) are as well advertised as one might expect, but information on these approaches are available in several locations.One source of information on using
depends-on to address this issue is the Spring forum message Spring, JMX, and RMI. Another source describing this is the Javadoc-generated API documentation for the RmiRegistryFactoryBean class. This class description includes this paragraph: "Also useful to enforce creation of a local RMI registry at a given port, for example for a JMX connector. If used in conjunction with ConnectorServerFactoryBean, it is recommended to mark the connector definition (ConnectorServerFactoryBean) as 'depends-on' the registry definition (RmiRegistryFactoryBean), to guarantee starting up the registry first."The Spring 2.5 Reference does not address the
depends-on attribute or importance of ordering in its JMX section, but it does describe that depends-on is occasionally necessary.






