Java 条件弹簧配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23872644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:45:51  来源:igfitidea点击:

Conditional Spring configuration

javaspring

提问by Frizz

Is it possible to use conditional expressions in a Spring config?

是否可以在 Spring 配置中使用条件表达式?

E.g. I'd like to define two different connectors like this:

例如,我想像这样定义两个不同的连接器:

Connector 1:

连接器 1:

<spring:bean id="MyConnector" class="org.test.provider.DBConnector">
    <spring:property name="host" value="${my.config.host}"/>
    <spring:property name="user" value="${my.config.user}"/>
    <spring:property name="password" value="${my.config.password}"/>
</spring:bean>

Connector 2:

连接器 2:

<spring:bean id="MyConnector" class="org.test.provider.FileSystemConnector">
    <spring:property name="path" value="${my.config.path}"/>
</spring:bean>

Then, later, use one of those like this:

然后,稍后,使用其中之一:

<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
    scope="prototype">
    <spring:property name="connector" ref="MyConnector"/>
</spring:bean>

Depending on, lets say, ${my.config.connectorType}from my .cfg file, I'd like to chose/activate one of those two:

可以说,${my.config.connectorType}根据我的 .cfg 文件,我想选择/激活这两个之一:

if ${my.config.connectorType} == DB then

    <spring:bean id="MyConnector" class="org.test.provider.DBConnector">
        <spring:property name="host" value="${my.config.host}"/>
        <spring:property name="user" value="${my.config.user}"/>
        <spring:property name="password" value="${my.config.password}"/>
    </spring:bean>

else

    <spring:bean id="MyConnector" class="org.test.provider.FileSystemConnector">
        <spring:property name="path" value="${my.config.path}"/>
    </spring:bean>
end
...
<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
    scope="prototype">
    <spring:property name="connector" ref="MyConnector"/>
</spring:bean>

回答by GokcenG

Just create 2 different property files. Let's say they have name DB.propertiesand filesystem.properties. After that by using property-placeholderyou can refer to your property files by this:

只需创建 2 个不同的属性文件。假设他们有名字DB.propertiesfilesystem.properties。之后,通过使用,property-placeholder您可以通过以下方式引用您的属性文件:

 <context:property-placeholder location="classpath*:META-INF/config/${my.config.connectorType}.properties"/>

If you start your application with '-Dmy.config.connectorType=DB' JVM parameter, then DB.propertiesfile will be loaded.

如果您使用 '-Dmy.config.connectorType=DB' JVM 参数启动应用程序,DB.properties则会加载文件。

<spring:bean id="MyDbConnector" class="org.test.provider.DBConnector" lazy-init="true">
    <spring:property name="host" value="${my.config.host}"/>
    <spring:property name="user" value="${my.config.user}"/>
    <spring:property name="password" value="${my.config.password}"/>
</spring:bean>

<spring:bean id="MyFileSystemConnector" class="org.test.provider.FileSystemConnector" lazy-init="true">
    <spring:property name="path" value="${my.config.path}"/>
</spring:bean>

<alias name="${my.connector}" alias="MyConnector"/>

<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
scope="prototype">
    <spring:property name="connector" ref="MyConnector"/>
</spring:bean>

DB.properties:
my.connector=MyDbConnector
filesystem.properties:
my.connector=MyFileSystemConnector

DB.properties
my.connector=MyDbConnector
filesystem.properties
my.connector=MyFileSystemConnector

回答by Kalyan

A simple alternative solution. Give different names for each connector as below

一个简单的替代解决方案。为每个连接器指定不同的名称,如下所示

<spring:bean id="dbConnector" class="org.test.provider.DBConnector">
    <spring:property name="host" value="${my.config.host}"/>
    <spring:property name="user" value="${my.config.user}"/>
    <spring:property name="password" value="${my.config.password}"/>
</spring:bean>

<spring:bean id="fileConnector" class="org.test.provider.FileSystemConnector">
    <spring:property name="path" value="${my.config.path}"/>
</spring:bean>

In your properties file, specify the name of the connector you wish to connect like my.config.connectorType=dbConnector

在您的属性文件中,指定您希望连接的连接器的名称,例如 my.config.connectorType=dbConnector

In LookupCommand bean, refer this property as below

在 LookupCommand bean 中,引用此属性如下

<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
    scope="prototype">
    <spring:property name="connector" ref="${my.config.connectorType}"/>
</spring:bean>

Note: I initially thought of suggesting bean definition profilebut you have to pass system properties -Dspring.profiles.activein your JVM. I'm trying to avoid that and in the above method you don't have the hassle to set any JVM system properties.

注意:我最初想建议bean 定义配置文件,但您必须-Dspring.profiles.active在 JVM 中传递系统属性。我试图避免这种情况,在上述方法中,您无需设置任何 JVM 系统属性。

回答by Jens Hoffmann

Another alternative approach: Bean definition profiles. Have these nested <beans>elements in your XML file:

另一种替代方法:Bean 定义配置文件<beans>在您的 XML 文件中有这些嵌套元素:

<beans profile="db1">
    <bean id="MyConnector" ...>
        ...
    </bean>
</beans>

<beans profile="db2">
    <bean id="MyConnector" ...>
        ...
    </bean>
</beans>

and add spring.profiles.activeto your environment variables like this:

并添加spring.profiles.active到您的环境变量中,如下所示:

-Dspring.profiles.active="db1"

回答by Ntokozo Zwane

For anyone still looking for a solution, I feel thisis an answer that speaks closest to the desired behaviour - using a ternary operator in the bean definition.

对于仍在寻找解决方案的任何人,我觉得是一个最接近所需行为的答案 - 在 bean 定义中使用三元运算符。