Learn to inject final static fields in some Spring bean into another beans using <util:constant> tag.

Spring util:constant Example

Logically, you will beans in below given manner. It has two static final fields ie. MANAGER and DIRECTOR.

public class EmployeeDTO 
    public static final EmployeeDTO MANAGER = new EmployeeDTO("manager");

    public static final EmployeeDTO DIRECTOR = new EmployeeDTO("director");

    private Integer id;
    private String firstName;
    private String lastName;
    private String designation;

    public EmployeeDTO(String designation) 
        this.id = -1;
        this.firstName = "dummy";
        this.lastName = "dummy";
        this.designation = designation;

    //Setters and Getters

    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName
                + ", lastName=" + lastName + ", type=" + designation + "]";

You would like to use above fields in EmployeeTypeWrapper class as below:

<util:constant id="MANAGER"
        static-field="com.howtodoinjava.demo.model.EmployeeDTO.MANAGER" />

<util:constant id="DIRECTOR"
    static-field="com.howtodoinjava.demo.model.EmployeeDTO.DIRECTOR" />

<!-- Use the static final bean constants here -->
<bean name="employeeTypeWrapper" class="com.howtodoinjava.demo.factory.EmployeeTypeWrapper">
    <property name="manager" ref="MANAGER" />
    <property name="director" ref="DIRECTOR" />

Where EmployeeTypeWrapper looks like this.

public class EmployeeTypeWrapper {

    private EmployeeDTO manager = null;

    private EmployeeDTO director = null;

    public EmployeeDTO getManager() {
        return manager;

    public void setManager(EmployeeDTO manager) {
        this.manager = manager;

    public EmployeeDTO getDirector() {
        return director;

    public void setDirector(EmployeeDTO director) {
        this.director = director;


Now test above configuration.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

EmployeeTypeWrapper employeeTypeWrapper = (EmployeeTypeWrapper) context.getBean("employeeTypeWrapper");



Employee [id=-1, firstName=dummy, lastName=dummy, type=manager]
Employee [id=-1, firstName=dummy, lastName=dummy, type=director]

Definitely, you can use the field references directly into your code as they are static fields. But having them defined and used through context file gives you flexibility to change the implementation from MANAGER to DIRECTOR any point of time without compiling the source-code.

Drop me your questions in comments section.

Happy Learning !!