Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

java - Spring bean initialization with multiple-arg method

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...