bean定义中使用的Spring表达式语言(SpEL)中的条件

发布于 2024-10-03 23:04:25 字数 331 浏览 0 评论 0原文

至于Spring 3.0中使用SpEL,

我想问一下,是否可以执行以下操作(在bean定义.xml中):

<c:choose>

  <c:when test="#{prop=='a'}">
   <bean class="BeanA"/>
  </c:when>

  <c:otherwise>
   <bean class="BeanB"/>
  </c:otherwise>

</c:choose>

一些。就像jstl中一样。

谢谢你的帮助。

As far SpEL is used in Spring 3.0,

I would like to ask, is it possible to do following(in bean definition .xml):

<c:choose>

  <c:when test="#{prop=='a'}">
   <bean class="BeanA"/>
  </c:when>

  <c:otherwise>
   <bean class="BeanB"/>
  </c:otherwise>

</c:choose>

Someth. like in jstl.

Thank you for help.

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

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

发布评论

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

评论(3

遗忘曾经 2024-10-10 23:04:25

环境配置文件/环境特定的 bean 将在 Spring 3.1 中提供,该版本应该很快就会发布 - 所以你可能需要等待。

Spring 3.0 中没有对条件 bean 的内置支持。但是,它可以通过使用 PropertyPlaceholderConfigurers 和/或 FactoryBeans 来实现。

Environment profiles/Environment specific beans will be available in Spring 3.1 which should be released shortly - so you might want to wait for that.

There is no built in support for conditional beans in Spring 3.0. However, it could be achieved by using PropertyPlaceholderConfigurers and/or FactoryBeans.

℡寂寞咖啡 2024-10-10 23:04:25

XML Spring bean 定义文件没有条件机制。
然而,也许这会起作用:

<bean class="#{prop=='a' ? BeanA : BeanB}"/>

但即使这种方法有效,它也不会是最具可读性的方法。我的建议是使用不同的 XML 配置文件集,并根据某些全局设置来选择它们。当然,您会将所有通用 bean(即定义始终相同的 bean)放在一个单独的文件中,并始终将其包含在内。

There's no conditional mechanism for XML Spring bean defintion files.
However, maybe this would work:

<bean class="#{prop=='a' ? BeanA : BeanB}"/>

But even if this approach worked, it wouldn't be the most readable one. My suggestion would be to use different set of XML configuration files and pick them depending on some global settings. Naturally you would put all the common beans (i.e. these whose definition is always the same) in a separate file and have it always included.

牵你手 2024-10-10 23:04:25

这不是使用 spel 的问题,而是更多 XML 的问题,据我所知,您不能在 XML 中执行此操作(但 xslt),

这种情况下正确的 spring 方式可能是 http://static.springsource.org/spring/docs/3.0.x /spring-framework-reference/html/beans.html#beans-factory-class 结合 BeanA 和 BeanB 的“父”接口,

您可以将参数(系统?运行时特定?)传递给工厂,这将创建 BeanA 或 BeanB

it is not a question of using spel, but more of XML,afaik you can't do this in XML (but xslt)

the proper spring way for this scenario might be http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-class in combination with a "parent" interface for BeanA and BeanB

you could pass the parameter (system ? runtime specific ?) to the factory, which would create either BeanA or BeanB

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