在 Spring xml 配置中访问 SpEL 中的应用程序属性 [英] Access application properties within SpEL in Spring xml configuration

查看:52
本文介绍了在 Spring xml 配置中访问 SpEL 中的应用程序属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据应用程序属性配置一个 spring bean,我的最终目标在以下伪代码中进行了描述:

I'm trying to configure a spring bean based on an application property, my end goal is described in the following pseudo code:

if ${my.config}
    <bean id="myBean" class="path.to.MyBeanImplOne" />
else
    <bean id="myBean" class="path.to.MyBeanImplTwo" />
end

其中 my.config 是一个布尔属性.根据 this SpEL 指南,#{${my.config} ?'path.to.MyBeanImplOne' : 'path.to.MyBeanImplTwo'} 是一个有效的表达式,所以我尝试了以下配置:

where my.config is a boolean property. According to this SpEL guide, #{${my.config} ? 'path.to.MyBeanImplOne' : 'path.to.MyBeanImplTwo'} is a valid expression, so I tried the following configuration:

<bean id="myBean" class="#{${my.config} ? 'path.to.MyBeanImplOne' : 'path.to.MyBeanImplTwo'}" />

但得到以下异常:

Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'lcurly({)'

我找不到用于访问 SpEL 表达式中用于 xml 配置的属性的文档.这是否仅在 Java 配置中受支持?

I can't find documentation for accessing properties in SpEL expressions for xml configuration. Is this supported only in Java configuration?

我已经看到了许多针对我的问题提出的解决方案(其中一些在这个问题).我不想使用 systemProperties 因为我觉得这种配置不应该指定一个运行参数,我觉得使用 profiles 对于这个特定用例来说太过分了.

I've seen a number of proposed solutions to my problem (some of which are in this question). I'd like to not use systemProperties since I feel this sort of configuration should not be specified a run arguments, and I feel the use of profiles is overkill for this particular use case.

有人能够成功地完成我尝试过的事情吗?或者有人可以确认xml配置中是否确实不支持我尝试使用的语法.

Has someone been able to do what I've attempted successfully? Or can someone confirm whether or not the syntax I've tried to use is indeed not supported in xml configuration.

推荐答案

尝试

class="#{'${my.config}'.equals('true') ? 'path.to.MyBeanImplOne' : 'path.to.MyBeanImplTwo'}"

编辑

这对我有用...

<bean id="foo" class="#{'${my.config}'.equals('true') ? 'java.lang.Integer' : 'java.lang.String'}">
    <constructor-arg value="1" />
</bean>

这篇关于在 Spring xml 配置中访问 SpEL 中的应用程序属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆