条件弹簧配置
是否可以在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>
连接器 2:
<spring:bean id="MyConnector" class="org.test.provider.FileSystemConnector">
<spring:property name="path" value="${my.config.path}"/>
</spring:bean>
然后,稍后,使用其中一个,如下所示:
<spring:bean id="LookupCommand" class="org.test.lookup.LookupCommand"
scope="prototype">
<spring:property name="connector" ref="MyConnector"/>
</spring:bean>
比方说,根据我的.cfg文件,我想选择/激活这两个文件之一:${my.config.connectorType}
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>