简体   繁体   中英

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.

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']}"/>

Here is the complete answer to the question. Keeping the override of user properties over default properties. My edit of the accepted answer got rejected.

<context:property-placeholder properties-ref="springContextCongifurationProperties"
                              location="classpath:/path/to/package/default.properties"
                              local-override="true"/>

<util:properties id="springContextCongifurationProperties"
                 location="classpath:/path/to/package/#{ systemProperties['user.name'] }.properties"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM