Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring bean initialization with multiple-arg method

Tags:

java

spring

I would like to create the following Spring bean (a JMX monitor) which has a method setThresholds(Number highThreshold,Number lowThreshold).

Could I invoke the method (with two arguments) in the configuration? I don't want to write codes to invoke it.

<bean id="myMonitor" class="javax.management.monitor.GaugeMonitor" init-method="start">
  <property name="observedObject">
    <bean class="javax.management.ObjectName">
      <constructor-arg value="test.jmx:name=testBean1" />
    </bean>
  </property>
  <property name="observedAttribute" value="testProperty" />
  <property name="granularityPeriod">
    <bean class="java.lang.Float">
      <constructor-arg value="1000" />
    </bean>
  </property>
</bean>
like image 648
Tommy Siu Avatar asked Mar 18 '11 07:03

Tommy Siu


1 Answers

It is possible by using the MethodInvokingFactoryBean (Spring 4.x and 5.x) (It is not my idea, I just found it this forum: http://forum.springsource.org/archive/index.php/t-16354.html)

SomeClass someobject = new SomeClass();
someobject.set("String1","String2");

<bean id="someobject" class="SomeClass" />

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="someobject">
    <property name="targetMethod" value="set">
    <property name="arguments">
        <list>
            <value>String1</value>
            <value>String2</value>
        </list>
    </property>
</bean>
like image 197
Ralph Avatar answered Oct 12 '22 22:10

Ralph