what's the best way to use application constants in spring xml configuration?

14,098

You could use <util:constant> (See 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.

Another option is to use the Java configuration style, which is more natural (see 4.12 Java-based container configuration):

@Bean
public SomeBean myBean() {
    SomeBean bean = new SomeBean();
    bean.setProperty(EXAMPLE_CONSTANT);  // using a static import
    return bean;
}
Share:
14,098
richarbernal
Author by

richarbernal

Software Engineer at Cystelcom, Madrid Experience with Java, Hibernate, Spring, MS SQL Server, LDAP, WS, Tomcat, Jetty. Interested in continuous integration for good code quality.

Updated on June 17, 2022

Comments

  • richarbernal
    richarbernal almost 2 years

    I want to use my application constants within spring xml configuration.

    I know to do that with spring SpEl with something like this:

    <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?