在spring上下文中使用bean值作为属性

时间:2016-09-13 12:05:38

标签: java spring

我有条件地创建自己的财产:

<bean id="path" class="java.lang.String">
    <constructor-arg value="#{ systemProperties['mySystemProperty'] == 'A' ? 'pathB' : 'pathC'}" />
</bean>

后来我想在引用资源时使用它:

<import resource="${path}/my-webapp-config.xml" />    // doesn't work

<import resource="#{path}/my-webapp-config.xml" />    // doesn't work

但是这不起作用......我怎样才能引用&#34; path&#34;的bean值。在资源的路径?

我不想使用属性配置器(已经用于其他目的)。而且我也不想写自己的CustomPropertyConfigurer。我只想使用bean的值。这可能很容易吗?

修改

可以使用表达式

#{ systemProperties['mySystemProperty'] == 'A' ? 'pathB' : 'pathC'} 

直接在路径中,但问题是它没有那样工作。我假设,这是因为字符串比较不起作用:

#{ systemProperties['mySystemProperty'] == 'A' 

有人可以说如何在弹簧上下文中使用spEL进行字符串比较吗?

UPDATE 可能不是字符串比较不起作用,但整行不做任何事情。我尝试了以下方式没有结果:

<import resource="#{ true ? 'pathB' : 'pathC'}/myConfig.xml"/>

实际上,资源永远不会被读取 - 并且也没有抱怨没有找到&#34; myConfig.xml&#34;等等。由于在那里定义了JNDI数据源,实际的错误消息始终是:

javax.naming.NameNotFoundException: Name [jdbc:mydatabasepath] is not bound in this Context

我也尝试过:

<import resource="#{systemProperties['mySystemProperty']}/myConfig.x‌​ml" /> 

得到了错误:

[file:/home/tomcat/....../#{systemProperties['mySystemProperty']}/myConfig.xml] is invalid; ... org.xml.sax.SAXParseException: Content is not allowed in prolog.

更新

我无法使其正常工作,但我现在正在使用弹簧配置文件。这更适合我的情况而且更容易。

看起来像这样:

<beans profile="live">
    <!-- include live resources --/>
</beans>

<beans profile="!live">
    <!-- include dev resources --/>
</beans>

tomcat以参数:

启动
-Dspring.profiles.active=dev

0 个答案:

没有答案