java 在 spring xml 配置中使用应用程序常量的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10619789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the best way to use application constants in spring xml configuration?
提问by richarbernal
I want to use my application constants within spring xml configuration.
我想在 spring xml 配置中使用我的应用程序常量。
I know to do that with spring SpEl with something like this:
我知道用 spring SpEl 做这样的事情:
<bean class="example.SomeBean">
<property name="anyProperty" value="#{ T(example.AppConfiguration).EXAMPLE_CONSTANT}" />
<!-- Other config -->
</bean>
So, is there a better way to do this?
那么,有没有更好的方法来做到这一点?
回答by skaffman
You could use <util:constant>
(See C.2.2 The util schema):
您可以使用<util:constant>
(参见C.2.2 The util schema):
<bean class="example.SomeBean">
<property name="anyProperty">
<util:constant static-field="example.AppConfiguration.EXAMPLE_CONSTANT" />
</property>
</bean>
It's debatable as to whether that's any better, though. Your SpEL version is more succinct.
不过,这是否更好,这是有争议的。您的 SpEL 版本更简洁。
Another option is to use the Java configuration style, which is more natural (see 4.12 Java-based container configuration):
另一种选择是使用Java配置风格,更自然(参见4.12基于Java的容器配置):
@Bean
public SomeBean myBean() {
SomeBean bean = new SomeBean();
bean.setProperty(EXAMPLE_CONSTANT); // using a static import
return bean;
}