Spring - using static final fields (constants) for bean initialization

SpringDefinitionJavabeans

Spring Problem Overview


is it possible to define a bean with the use of static final fields of CoreProtocolPNames class like this:


<bean id="httpParamBean" class="org.apache.http.params.HttpProtocolParamBean">
	 <constructor-arg ref="httpParams"/>
	 <property name="httpElementCharset" value="CoreProtocolPNames.HTTP_ELEMENT_CHARSET" />
	 <property name="version" value="CoreProtocolPNames.PROTOCOL_VERSION">
</bean>

public interface CoreProtocolPNames {

    public static final String PROTOCOL_VERSION = "http.protocol.version"; 

    public static final String HTTP_ELEMENT_CHARSET = "http.protocol.element-charset"; 
}

If it is possible, what is the best way of doing this ?

Spring Solutions


Solution 1 - Spring

Something like this (Spring 2.5)

<bean id="foo" class="Bar">
    <property name="myValue">
        <util:constant static-field="java.lang.Integer.MAX_VALUE"/>
    </property>
</bean>

Where util namespace is from xmlns:util="http://www.springframework.org/schema/util"

But for Spring 3, it would be cleaner to use the @Value annotation and the expression language. Which looks like this:

public class Bar {
    @Value("T(java.lang.Integer).MAX_VALUE")
    private Integer myValue;
}

Solution 2 - Spring

Or, as an alternative, using Spring EL directly in XML:

<bean id="foo1" class="Foo" p:someOrgValue="#{T(org.example.Bar).myValue}"/>

This has the additional advantage of working with namespace configuration:

<tx:annotation-driven order="#{T(org.example.Bar).myValue}"/>

Solution 3 - Spring

don't forget to specify the schema location..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.1.xsd">


</beans>

Solution 4 - Spring

One more example to add for the instance above. This is how you can use a static constant in a bean using Spring.

<bean id="foo1" class="Foo">
  <property name="someOrgValue">
    <util:constant static-field="org.example.Bar.myValue"/>
  </property>
</bean>

package org.example;

public class Bar {
  public static String myValue = "SOME_CONSTANT";
}

package someorg.example;

public class Foo {
    String someOrgValue; 
    foo(String value){
        this.someOrgValue = value;
    }
}

Solution 5 - Spring

<util:constant id="MANAGER"
        static-field="EmployeeDTO.MANAGER" />
 
<util:constant id="DIRECTOR"
    static-field="EmployeeDTO.DIRECTOR" />
 
<!-- Use the static final bean constants here -->
<bean name="employeeTypeWrapper" class="ClassName">
    <property name="manager" ref="MANAGER" />
    <property name="director" ref="DIRECTOR" />
</bean>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlisakView Question on Stackoverflow
Solution 1 - SpringPaul McKenzieView Answer on Stackoverflow
Solution 2 - Springcr7pt0gr4ph7View Answer on Stackoverflow
Solution 3 - SpringSampath PasupunuriView Answer on Stackoverflow
Solution 4 - SpringBalaji Boggaram RamanarayanView Answer on Stackoverflow
Solution 5 - Springchetan singhalView Answer on Stackoverflow