2

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.x‌​ml" /> 

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
Nina
  • 681
  • 1
  • 10
  • 27
  • Have you tried tweaking the searchSystemEnvironment and systemPropertiesModeName properties of the existing placeholder configuration? It will allow you to use your System properties with the placeholder syntax. –  Sep 13 '16 at 12:15
  • I am not sure what you mean by that. Can you be more specific, please? – Nina Sep 13 '16 at 12:17
  • There's a provision in PropertyPlaceholderConfigurer to include System properties. It would allow you to use System properties using placeholder syntax. Something like ${mySystemProperty}. Will that be useful for you in anyways? –  Sep 13 '16 at 12:20
  • I can't use mySystemProperty directly because I am creating the path conditionally. I only need to know how to create a property conditionally and use it later as a normal property. – Nina Sep 13 '16 at 12:21
  • Ok, Is that intermediate bean necessary? Doesn't this work? –  Sep 13 '16 at 12:24
  • Mmh... it's an idea... Right now I only use "path" two times, so it would be nice to have a property but not absolutely necessary. I tried it, can't yet make it all working but it seems to work that way generally. – Nina Sep 13 '16 at 12:40
  • The string comparison #{ systemProperties['mySystemProperty'] == 'A' does not seem to work. – Nina Sep 14 '16 at 07:17
  • 1
    Ok. Please refer to this post http://stackoverflow.com/questions/23650771/can-spel-be-used-with-import-statements-in-spring-xml-configuration I was able to evaluate your SPEL successfully in other tags except import. –  Sep 20 '16 at 19:46

0 Answers0