arg方法初始化bean

arg方法初始化bean

本文介绍了使用multi-arg方法初始化bean bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建以下Spring bean(一个JMX监视器),它有一个方法 setThresholds(Number highThreshold,Number lowThreshold)

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>


推荐答案

可以使用(Spring 和)(这不是我的想法,我只是发现了这个论坛:)

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>

这篇关于使用multi-arg方法初始化bean bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 20:17