HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Core / Spring util:constant to refer final static field references

Spring util:constant to refer final static field references

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

    @Override
    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" />
</bean>

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;
    }
}

Demo

Now test above configuration.

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

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

System.out.println(employeeTypeWrapper.getManager());
System.out.println(employeeTypeWrapper.getDirector());

Output:

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

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Swapna

    March 22, 2019

    What is the exact use of this concept in real time because we have another options to inject beans through annotations and xml.

  2. giridhar

    January 29, 2016

    to get util tag we need to update the schema :

Comments are closed on this article!

Search Tutorials

Spring Tutorial

  • Spring – Introduction
  • Spring – IoC Containers
  • Spring – IoC vs. DI
  • Spring – Bean Scopes
  • Spring – Bean Life Cycle
  • Spring – Bean Postprocessors
  • Spring – Autowiring
  • Spring – Annotations
  • Spring – Stereotype Annotations
  • Spring – Task Scheduling
  • Spring – Timer Task
  • Spring – Events
  • Spring – Message Source
  • Spring – ResourceLoader
  • Spring – Property Editor
  • Spring – Send Email
  • Spring – Version-less Schema
  • Spring – Interview Questions
  • Spring – Best Practices

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces