복붙노트

[SPRING] 싱글 톤 대신 Pooled Spring Bean을 사용하는 방법?

SPRING

싱글 톤 대신 Pooled Spring Bean을 사용하는 방법?

효율성을 위해 Spring 애플리케이션 컨텍스트의 빈을 동시에 사용하는 스레드의 수를 제한하는 것에 관심이 있습니다 (제한된 메모리에서 스레드를 무제한으로 처리하는 것을 원하지 않습니다).

나는 여기 (봄 문서)에서 EJB 스타일로 빈을 풀링함으로써 이것을 달성하는 방법을 발견했다.

이 콩 선언은 다음과 같습니다.

<bean id="businessObjectTarget" class="com.mycompany.MyBusinessObject" 
    scope="prototype">
  ... properties omitted
</bean>

<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
  <property name="targetBeanName" value="businessObjectTarget"/>
  <property name="maxSize" value="25"/>
</bean>

<bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="targetSource" ref="poolTargetSource"/>
  <property name="interceptorNames" value="myInterceptor"/>
</bean>

내 문제는 "businessObjectTarget"의 풀링 된 인스턴스를 사용하기 위해 다른 빈을 선언 할 때 어떻게해야 하는가? 내 말은, 내가 이런 식으로하려고 할 때 :

<bean id="clientBean" class="com.mycompany.ClientOfTheBusinessObject">
  <property name="businessObject" ref="WHAT TO PUT HERE???"/>
</bean>

"ref"의 가치는 무엇입니까 ??

해결법

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

    1.프로퍼티를 사용하여 프로토 타입의 인스턴스를 가져올 수 없습니다. 하나의 옵션은 조회 방법을 사용하는 것입니다 (3.3.7.1 장 참조) 코드에서 bean을 얻는 또 다른 옵션 : com.mycompany.ClientOfTheBusinessObject가 ApplicationContextAware 인터페이스를 구현하도록 만든 다음 context.getBean ( "clientBean")을 호출한다.

    프로퍼티를 사용하여 프로토 타입의 인스턴스를 가져올 수 없습니다. 하나의 옵션은 조회 방법을 사용하는 것입니다 (3.3.7.1 장 참조) 코드에서 bean을 얻는 또 다른 옵션 : com.mycompany.ClientOfTheBusinessObject가 ApplicationContextAware 인터페이스를 구현하도록 만든 다음 context.getBean ( "clientBean")을 호출한다.

  2. ==============================

    2.덜 복잡한 방식으로 동시 스레드의 수를 제한 할 수 있다고 확신합니다. 특히 Executors.newFixedThreadPool ()에서 Java Concurrency API를 보셨습니까?

    덜 복잡한 방식으로 동시 스레드의 수를 제한 할 수 있다고 확신합니다. 특히 Executors.newFixedThreadPool ()에서 Java Concurrency API를 보셨습니까?

  3. ==============================

    3.나는 java-configuration을 사용하여 apache commons-pool을 사용하여 풀링을 처리하는 인터페이스를 통해 프록시를 구성하여 호출 수준 풀링을 달성했습니다.

    나는 java-configuration을 사용하여 apache commons-pool을 사용하여 풀링을 처리하는 인터페이스를 통해 프록시를 구성하여 호출 수준 풀링을 달성했습니다.

  4. ==============================

    4.spring 예제에서 세 번째 bean의 이름을 주목하라. - "businessObject"

    spring 예제에서 세 번째 bean의 이름을 주목하라. - "businessObject"

    이것은 당신이 공통 풀에 접근하기로되어있는 빈을 의미합니다.

    자신의 클라이언트 bean이 필요하다면 다음과 같이 할 수 있습니다. 그러나 그런 경우에는 비즈니스 오브젝트가 필요하지 않습니다 .-

    <bean id="businessObjectTarget" class="com.mycompany.MyBusinessObject" 
        scope="prototype">
    
      ... properties omitted
    </bean>
    
    <bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
      <property name="targetBeanName" value="businessObjectTarget"/>
      <property name="maxSize" value="25"/>
    </bean>
    
    <bean id="clientBean" class="com.mycompany.ClientOfTheBusinessObject">
     <property name="poolTargetSource" ref="poolTargetSource"/>
    </bean>
    
    
    Java classes:-
    public class ClientOfTheBusinessObject{
         CommonsPoolTargetSource poolTargetSource;
    //<getter and setter for poolTargeTSource>
      public void methodToAccessCommonPool(){
         //The following line gets the object from the pool.If there is nothing left in the pool then the thread will be blocked.(The blocking can be replaced with an exception by changing the properties of the CommonsPoolTargetSource bean)
         MyBusinessObject mbo = (MyBusinessObject)poolTargetSource.getTarget();
         //Do whatever you want to do with mbo
         //the following line puts the object back to the pool
         poolTargetSource.releaseTarget(mbo);
      }
    }
    
  5. from https://stackoverflow.com/questions/6493583/how-to-use-pooled-spring-beans-instead-of-singleton-ones by cc-by-sa and MIT license