条件弹簧配置

Fri*_*izz 12 java spring

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

例如,我想定义两个不同的连接器,如下所示:

连接器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>
Run Code Online (Sandbox Code Playgroud)

连接器2:

<spring:bean id="MyConnector" class="org.test.provider.FileSystemConnector">
    <spring:property name="path" value="${my.config.path}"/>
</spring:bean>
Run Code Online (Sandbox Code Playgroud)

然后,稍后使用以下其中一个:

<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
    scope="prototype">
    <spring:property name="connector" ref="MyConnector"/>
</spring:bean>
Run Code Online (Sandbox Code Playgroud)

根据${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>
Run Code Online (Sandbox Code Playgroud)

Kal*_*yan 5

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
    scope="prototype">
    <spring:property name="connector" ref="${my.config.connectorType}"/>
</spring:bean>
Run Code Online (Sandbox Code Playgroud)

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


Gok*_*enG 1

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

 <context:property-placeholder location="classpath*:META-INF/config/${my.config.connectorType}.properties"/>
Run Code Online (Sandbox Code Playgroud)

如果您使用“-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>
Run Code Online (Sandbox Code Playgroud)

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