2017-05-31 9 views
0

私は2つの異なる処理を行っています。conditionに基づいてSpringのinitメソッドを呼び出す

JVM引数にプロセス名を渡しています。その引数を使用すると、いずれかのプロセスが呼び出されます。

my app context XML。

<bean id="propertyPlaceHolderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath*:properties/${processJVMArg}.properties</value> 
      </list> 
     </property> 
    </bean> 
<bean id="splitService" class="com.split.service.SplitService" init-method="process1"><!-- "based on processJVMArg JVM argument should call process1 or process2. " --> 

複数のinitメソッドを設定する方法はありますか?initメソッドは、通信に基づいて呼び出す必要がありますか?

おかげで、

ラマ

+0

*伝導*に基づいていますか?プログラミングではどういう意味ですか? – Andreas

+0

SpringのProfile機能を使用して調べる必要があります。 Spring Frameworkリファレンスドキュメント - [Chapter 7.13 Environment abstraction](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-environment)を参照してください。 – Andreas

答えて

2

春は、プロファイルを持っています。一連の条件に基づいて、正確なプロファイルでアプリを起動できます。 Bean beanCreatingBasedOnProfiledevのプロファイルでのみ作成されます。

@Bean 
@Profile("dev") 
public YourClass beanCreatingBasedOnProfile() { 
    return new YourClass(); 
} 

また、春は Conditional Beansです。 プロパティ値などに基づいてBeanを構築できます。例:

@ConditionalOnProperty(prefix = "spring.prop", name = "dynamic", matchIfMissing = true) 
public YourClass condBean() { 
    return new YourClass(); 
} 
関連する問題