복붙노트

[SPRING] 다중 arg 메소드를 이용한 스프링 빈 초기화

SPRING

다중 arg 메소드를 이용한 스프링 빈 초기화

setThresholds (Number highThreshold, Number lowThreshold) 메소드를 가진 다음 Spring 빈 (JMX 모니터)을 만들고 싶습니다.

구성에서 (두 개의 인수로) 메소드를 호출 할 수 있습니까? 나는 그것을 호출하는 코드를 작성하고 싶지 않다.

<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>

해결법

  1. ==============================

    1.MethodInvokingFactoryBean (Spring 4.x 및 5.x)을 사용하는 것이 가능합니다. (제 생각에는 방금이 포럼을 발견했습니다 : http://forum.springsource.org/archive/index.php/t-16354 .html)

    MethodInvokingFactoryBean (Spring 4.x 및 5.x)을 사용하는 것이 가능합니다. (제 생각에는 방금이 포럼을 발견했습니다 : 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>
    
  2. ==============================

    2.나는 이것을 한 번도 본 적이 없다. Spring의 가장 중요한 아이디어는 바로 앞으로 콩을 만들고 초기화한다는 것입니다. 그러므로 호출 될 유일한 메소드는 단일 인수 Setters (...) 및 생성자입니다. 지원되는 정의는 다음 스키마에 있습니다.

    나는 이것을 한 번도 본 적이 없다. Spring의 가장 중요한 아이디어는 바로 앞으로 콩을 만들고 초기화한다는 것입니다. 그러므로 호출 될 유일한 메소드는 단일 인수 Setters (...) 및 생성자입니다. 지원되는 정의는 다음 스키마에 있습니다.

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

    이 문제를 해결할 수있는 방법은 빈을 초기화하여 InitializingBean을 구현하고 void afterPropertiesSet () 메소드에서 메소드를 호출하는 것입니다.

    예 :

    public void setHighThreadHold(Number highThreshHold) {}
    
    public void setLowThreashHold(Number lowThreadHold) {}
    
    
    public void afterPropertiesSet() {
        setThresholds(highThreshold,lowThreshold);
    }
    
  3. from https://stackoverflow.com/questions/5349362/spring-bean-initialization-with-multiple-arg-method by cc-by-sa and MIT license