使用multi-arg方法初始化bean bean

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

我可以在配置中调用方法(带两个参数)吗? 我不想编写代码来调用它。

             

可以通过使用MethodInvokingFactoryBean (Spring 4.x和5.x )(这不是我的想法,我只是在这个论坛找到它: http : //forum.springsource.org/archive/index.php/t-16354 .html )

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

       String1 String2    

我从来没有见过这样做过。 Spring的主要思想是你创建并初始化直接的bean。 因此,唯一要调用的方法是单个参数Setters(…)和Constructors。 支持内容的定义将在以下模式中:

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

解决这个问题的方法是让你的bean实现InitializingBean并在void afterPropertiesSet()方法中调用你的方法:

例如:

 public void setHighThreadHold(Number highThreshHold) {} public void setLowThreashHold(Number lowThreadHold) {} public void afterPropertiesSet() { setThresholds(highThreshold,lowThreshold); }