2017-03-12 10 views
1

내 질문은 클러스터 환경에서 다중 쓰레드를 실행하는 자바 스프링 애플리케이션에 사용해야하는 구성 전략에 관한 질문이다.모듈 프로젝트에서 같은 bean id의 bean 프라퍼티를 상속하고 오버라이드하기

응용 프로그램의 계층 구조는 다음과 같이이다 :

@Transactional 
public class DataProvider { 

    protected Configuration configuration; 

    public DataProvider(Configuration configuration, DummyArg1 arg1, DummyArg2 arg2) { 
     this.configuration = configuration; 
     createResult(); 
     addMoreStuffFromModules(); 
    } 

    private void createResult() { 
     this.result.setSomeStuff = "someStuff"; 
    } 

    private void addMoreStuffFromModules() { 
     this.result.setSomeMoreStuff = configuration.getModuleDataProvider.getData(); 
    } 
} 

메인 응용 프로그램 구성 :

<bean id="dataProvider" abstract="true" class="com.main.DataProvider"> 
     <constructor-arg ref="main-configuration"/> 
     <constructor-arg ref="main-dummyArg1"/> 
     <constructor-arg ref="main-dummyArg2"/> 
</bean 

<bean id="main-Configuration" class="com.main.Configuration" /> <!-- just an empty class in main application --> 

부모 모듈 구성

Main application files & config 
| 
|__Parent module files & config 
    | 
    |__Child module files & config 

나는 @Transactional하는 DataProvider를이 :

<bean id="dataProvider" abstract="true" class="com.main.DataProvider"> 
     <constructor-arg ref="parent-configuration"/> 
     <constructor-arg ref="parent-dummyArg1"/> 
     <constructor-arg ref="main-dummyArg2"/> 
</bean 

<bean id="parent-Configuration" class="com.parent.Configuration" /> 

알 수 있습니다. 부모 모듈은 dataProvider에 대한 새 bean과 모든 생성자 인수를 제공하여 자체 구성을 데이터 공급자 클래스에 삽입 할 수 있어야합니다.

<bean id="dataProvider" abstract="true" class="com.main.DataProvider"> 
     <constructor-arg ref="child-configuration"/> 
     <constructor-arg ref="parent-dummyArg1"/> 
     <constructor-arg ref="main-dummyArg2"/> 
</bean 

<bean id="child-Configuration" class="com.child.Configuration" /> 

이 좋은 전략이다 : 이 하위 모듈에 대해 동일합니다? 나는 그렇지 않다고 생각한다. 상위 구성이 업데이트되면 (예 : 구성이 다른 빈을 참조하도록 설정하는 경우) 어떤 일이 발생합니까? 그런 다음 자식 구성이 constructor-arg에 새 참조를 자동으로 가져 오도록해야합니다.

는 가능하면이 같은 예를 들어, 그들은 (안 다른 인수)을 변경하기 원하는 모듈 만 생성자 인수를 설정의 구성을 가지고, 선호 :

<bean id="dataProvider" abstract="true" class="com.main.DataProvider"> 
     <constructor-arg ref="child-configuration"/> 
     <!-- arg1 inherited from parent and/or main configuration --> 
     <!-- arg2 inherited from parent and/or main configuration--> 
</bean 

<bean id="child-Configuration" class="com.child.Configuration" /> 

어떤 아이디어에 어떻게 이것을 달성 하는가?

답변

1

Configuration을 빈으로 만들면 하위 모듈에서 해당 빈을 재정의하고 DataProvider 빈을 겹쳐 쓰지 않아도됩니다.

업데이트

자바 기반의 구성 예, 나는 XML의 CONFIGS를 사용하지 않는 :

당신이 DataProvider에 필요한 3 콩 있다고 가정하자 :

@Component 
public class Configuration { 
    // main config here 
} 

@Component 
public class OtherData { 
} 

@Component 
public class MoreData { 
} 

및 데이터 제공 업체 등이 이 :

@Component @Primary 
public class ChildConfiguration extends Configuration { 
    // different config here 
} 

을 ... 당신은 다른 두 부분을 변경하지 않고 단지 구성 부분을 오버라이드 (override) 할 것이다 :그런 다음 다른 모듈에 않습니다.

+0

완전히 이해하지 못했습니다. 예제를 추가 할 수 있습니까? – henrik

+0

@Component 주석은 기본적으로 작동합니까? 또는 어노테이션 스캐닝을 어떻게해야합니까? – henrik

+0

부모 모듈과 자식 모듈 모두 구성을 재정의하려면 어때요? 둘 다 수업에서 @Primary를 제공 할 수 있습니까? 그렇다면 어느 것이 선택 될까요? – henrik