19

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>
3
  • "I don't want to write codes to invoke it". Why not? Why do you want to do weird things in XML, instead of doing it Java, where it belongs? Commented Mar 18, 2011 at 8:41
  • @skaffman - For maintenance reason...I usually like to keep this kind of configuration details in XML. Commented Mar 18, 2011 at 9:18
  • Similar question: stackoverflow.com/questions/5312605/… Commented Mar 18, 2011 at 10:24

2 Answers 2

33

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>
Sign up to request clarification or add additional context in comments.

5 Comments

The factory bean has to be outside the "someObject"
@byeo: Yes, that is right. In the example above it was already outside (there is a '/' at the end of the someobject bean definition), but the indenting was confusing. -- I have improved the formatting now.
If you want to use this for configuration purposes, it seems pretty bloated to me. If you have twenty parameters to the application, you'll define 40 beans, 20 actual beans and 20 (not so easy to read, imho) factory beans. What I mean is that the configuration file is bloated. Still, I tried a JAVA solution and that also seemed rather bloated... :)
@Timo: If you have an better/other solution for the question, then post your own answer. But if you only want to complain about Spring, then please use some blog but not abuse the comment function.
@Ralph I don't have a better solution, else I would have posted it. I hoped to trigger you (i.e. anybody) to take up my remark and find a "better" solution where I didn't see it immediately. I understand the philosophy of Spring in this matter and did not mean to just complain. No offence meant!
1

I've never seen this done. The big idea of Spring is that you create and initialise straight forward beans. Therefore the only methods that will be called are therefore single argument Setters(...) and Constructors. The definition of what's supported will be in the following schema:

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Your way around this problem is to get your bean to implement InitializingBean and call your method in the void afterPropertiesSet() method:

eg:

public void setHighThreadHold(Number highThreshHold) {}

public void setLowThreashHold(Number lowThreadHold) {}


public void afterPropertiesSet() {
    setThresholds(highThreshold,lowThreshold);
}

1 Comment

As it is a J2SE class, I don't want to add a wrapper class just to invoke the method...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.