基于condiction调用Spring init方法

时间:2017-05-31 16:19:40

标签: java spring spring-bean

我正在处理两个不同的过程。

我在JVM参数中传递进程名称。使用该参数,任何一个进程都应该调用。

我的应用上下文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方法应该根据传导进行调用?

谢谢,

拉​​马

1 个答案:

答案 0 :(得分:2)

Spring有个人资料。根据一组条件,您可以使用确切的配置文件启动应用程序。 Bean beanCreatingBasedOnProfile将仅在dev个人资料中创建。

@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();
}
相关问题