Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring expression in xml configuration file

It is useful to have different property sets for different users.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:property-placeholder
        location="classpath:/path/to/package/default.properties,
        classpath:/path/to/package/#{ systemProperties['user.name'] }.properties"/>

</beans>

When executing the application, spring does not recognize the expression. The context does not start and spring says: class path resource [path/to/package/#{ systemProperties['user.name'] }.properties] cannot be opened

When I replace the expression manually with a string resulting in a valid resource then the behaviour is as expected. The manual states it should work.

The spring-context and spring-core (3.1.2-RELEASE) are in classpath.

  • How come spring does not pick up the environment variable?
  • I'm open to alternate solutions solving the same functional problem.
like image 220
Hector Avatar asked Nov 02 '12 17:11

Hector


1 Answers

SpEL expressions are not allowed there; you can do what you want indirectly, though...

<context:property-placeholder properties-ref="props"/>

<util:properties id="props" location="classpath:#{systemProperties['foo']}"/>
like image 119
Gary Russell Avatar answered Sep 30 '22 01:09

Gary Russell