I am creating my own property conditionally:
<bean id="path" class="java.lang.String">
<constructor-arg value="#{ systemProperties['mySystemProperty'] == 'A' ? 'pathB' : 'pathC'}" />
</bean>
Later I want to use it in referencing a resource:
<import resource="${path}/my-webapp-config.xml" /> // doesn't work
or
<import resource="#{path}/my-webapp-config.xml" /> // doesn't work
But this does not work that way... How can I reference the bean value of "path" in the path to a resource?
I don't want to use a property configurer (is already in use for other purpose). And I also don't want to write my own CustomPropertyConfigurer
. I only want to use the value of the bean. Is that possible in an easy way?
EDIT
It's possible to use the expression
#{ systemProperties['mySystemProperty'] == 'A' ? 'pathB' : 'pathC'}
directly in the path, but the problem is that it's not working that way. I assume, this is due to the fact that the string comparison does not work:
#{ systemProperties['mySystemProperty'] == 'A'
Can somebody say how to do a String comparison in a spring context with spEL?
UPDATE It is probably not the String comparison that does not work but the whole line does nothing. I tried it the following way without result:
<import resource="#{ true ? 'pathB' : 'pathC'}/myConfig.xml"/>
Actually, the resource is never read - and there is also no complain about not finding "myConfig.xml" or the like. As a JNDI-datasource is defined in there, the actual error message is always:
javax.naming.NameNotFoundException: Name [jdbc:mydatabasepath] is not bound in this Context
I also tried:
<import resource="#{systemProperties['mySystemProperty']}/myConfig.xml" />
And got the error:
[file:/home/tomcat/....../#{systemProperties['mySystemProperty']}/myConfig.xml] is invalid; ... org.xml.sax.SAXParseException: Content is not allowed in prolog.
UPDATE
I couldn't make it work, but I am using spring profiles instead now. That is much more appropriate for my case and much easier.
It looks like this:
<beans profile="live">
<!-- include live resources --/>
</beans>
<beans profile="!live">
<!-- include dev resources --/>
</beans>
The tomcat is started with the argument:
-Dspring.profiles.active=dev