我可以在 Spring EL 中使用属性占位符吗?

发布于 2024-09-06 13:44:47 字数 2158 浏览 7 评论 0原文

在升级到 Spring 3 之前,我的 applicationContext.xml 文件中有这样的内容:

    <bean class="com.northgateis.pole.ws.PolePayloadValidatingInterceptor">
      <property name="validateRequest" value="${validateRequest}" />
      <property name="validateResponse" value="${validateResponse}" />
    </bean>

其中 ${validateRequest) 和 ${validateRequest) 引用可能在我的属性文件中定义或未定义的属性。

在 Spring 2 中,如果属性文件中不存在这些属性,则不会调用 bean 上的 setter,因此使用 PolePayloadValidatingInterceptor 中硬编码的默认值。

升级到 Spring 3 后,行为似乎有所不同: 如果属性文件中不存在属性,则会出现以下异常:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'annotationMapping' defined in class path resource [com/northgateis/pole/ws/applicationContext-ws.xml]: Could not resolve placeholder 'validateRequest'
 at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
 at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
 at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)

我尝试使用 Spring EL 但以下内容似乎不起作用:

    <bean class="com.northgateis.pole.ws.PolePayloadValidatingInterceptor">
      <property name="validateRequest" value="${validateRequest?:true}" />
      <property name="validateResponse" value="${validateResponse?:false}" />
    </bean>

The value after the Elvis始终使用运算符,即使属性是在 proeprties 文件中定义的。有趣的是语法被接受了。

有什么建议吗?

Before upgrading to Spring 3 I had this in my applicationContext.xml file:

    <bean class="com.northgateis.pole.ws.PolePayloadValidatingInterceptor">
      <property name="validateRequest" value="${validateRequest}" />
      <property name="validateResponse" value="${validateResponse}" />
    </bean>

where ${validateRequest) and ${validateRequest) refer to properties that may or may not be defined in my properties file.

In Spring 2, if these proeprties were not present in the properties file the setters on the bean were not called and so the defaults hard-coded in PolePayloadValidatingInterceptor were used.

After upgrading to Spring 3, it seems the behaviour is different: If the properties are not present in the properties file I get the following exception:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'annotationMapping' defined in class path resource [com/northgateis/pole/ws/applicationContext-ws.xml]: Could not resolve placeholder 'validateRequest'
 at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272)
 at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:640)
 at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:615)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
 at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272)

I tried dabbling with Spring EL but the following doesn't seem to work:

    <bean class="com.northgateis.pole.ws.PolePayloadValidatingInterceptor">
      <property name="validateRequest" value="${validateRequest?:true}" />
      <property name="validateResponse" value="${validateResponse?:false}" />
    </bean>

The value after the Elvis operator is always used, even when the properties are defined in the proeprties file. Interesting that the syntax is accepted.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终弃我 2024-09-13 13:44:47

看起来 Spring 3 使用 Elvis 操作符处理默认值的方式相当糟糕。这显然已在 fresh-out-of- 中得到修复(请参阅 SPR-7209) the-oven Spring 3.0.3,正确的语法应该是相当巴洛克式的:

#{${validateRequest}?:true}

It looks like Spring 3's handling of default values with the Elvis operator was rather broken. This has apparently been fixed (see SPR-7209) in the fresh-out-of-the-oven Spring 3.0.3, and the correct syntax should be the rather baroque:

#{${validateRequest}?:true}
山色无中 2024-09-13 13:44:47

当使用占位符配置器解析缺少的属性时,Spring EL 不需要为它设置默认值。只需使用${validateRequest:true}。 “Elvis 运算符”不关心解析占位符,它只依赖于占位符配置器提供的任何输入。

请参阅 SPR-4785

There's no need for Spring EL for setting a default value for a missing property when resolving it with a placeholder configurer. Simply use ${validateRequest:true}. The "Elvis operator" is not concerned with resolving placeholders, it just relies on whatever input the placeholder configurer provides.

See SPR-4785.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文