【问题标题】:what's the best way to use application constants in spring xml configuration?在 Spring xml 配置中使用应用程序常量的最佳方法是什么?
【发布时间】:2012-05-16 13:38:35
【问题描述】:

我想在 spring xml 配置中使用我的应用程序常量。

我知道用 spring SpEl 来做这样的事情:

<bean class="example.SomeBean">
    <property name="anyProperty" value="#{ T(example.AppConfiguration).EXAMPLE_CONSTANT}" />
    <!-- Other config -->
</bean>

那么,有没有更好的方法来做到这一点?

【问题讨论】:

  • 也许我遗漏了一些东西,但为什么要在类中设置常量而不是在属性文件中?

标签: java spring constants spring-el


【解决方案1】:

你可以使用&lt;util:constant&gt;(见C.2.2 The util schema):

<bean class="example.SomeBean">
    <property name="anyProperty">
       <util:constant static-field="example.AppConfiguration.EXAMPLE_CONSTANT" />
    </property>
</bean>

不过,这是否更好还有待商榷。您的 SpEL 版本更简洁。

另一种选择是使用Java配置风格,更自然(见4.12 Java-based container configuration):

@Bean
public SomeBean myBean() {
    SomeBean bean = new SomeBean();
    bean.setProperty(EXAMPLE_CONSTANT);  // using a static import
    return bean;
}

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多