bean定义中使用的Spring表达式语言(SpEL)中的条件
至于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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
环境配置文件/环境特定的 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.
XML Spring bean 定义文件没有条件机制。
然而,也许这会起作用:
但即使这种方法有效,它也不会是最具可读性的方法。我的建议是使用不同的 XML 配置文件集,并根据某些全局设置来选择它们。当然,您会将所有通用 bean(即定义始终相同的 bean)放在一个单独的文件中,并始终将其包含在内。
There's no conditional mechanism for XML Spring bean defintion files.
However, maybe this would work:
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.
这不是使用 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