简体   繁体   English

xml配置文件中的Spring表达式

[英]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. 执行应用程序时,spring无法识别表达式。 The context does not start and spring says: class path resource [path/to/package/#{ systemProperties['user.name'] }.properties] cannot be opened 上下文没有启动,spring说: 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. spring-context和spring-core(3.1.2-RELEASE)在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; 那里不允许使用SpEL表达式; 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"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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