Use util:constant To Declare Final Static Field As Spring Bean

Spring tag util:constant can be used to declare a bean that references one class’s final static field. This article will show you how to do that.

1. Bound util namespace.

  1. To use util:constant, you should first bound the util namespace for beans tag in application context XML file ( BeanSettings.xml  ) as below.
  2. Below is the entire BeanSettings.xml file content.
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

2. Create

  1. This is java class represent car brand. It has two final static filed which are AUDI_BRAND and FORD_BRAND, the two fields will be referenced later.
    public class CarBrand {
    	public final static CarBrand AUDI_BRAND = new CarBrand("Audi" , "Germany");
    	public final static CarBrand FORD_BRAND = new CarBrand("Ford" , "United States");
    	private String brandName;
    	private String brandCountry;
    	public String getBrandName() {
    		return brandName;
    	public void setBrandName(String brandName) {
    		this.brandName = brandName;
    	public String getBrandCountry() {
    		return brandCountry;
    	public void setBrandCountry(String brandCountry) {
    		this.brandCountry = brandCountry;
    	public CarBrand(String brandName, String brandCountry) {
    		this.brandName = brandName;
    		this.brandCountry = brandCountry;
    	public String toString() {
    		StringBuffer retBuf = new StringBuffer();
    		retBuf.append("Brand Name : " + this.getBrandName());
    		retBuf.append(" , Brand Country : " + this.getBrandCountry());
    		return retBuf.toString();

3. Create

  1. This class represents a car factory that will produce Audi and Ford cars. It has two properties for Audi brand and Ford brand car.
    public class CarFactory {
    	private CarBrand audiBrand;
    	private CarBrand fordBrand;
    	public CarBrand getAudiBrand() {
    		return audiBrand;
    	public void setAudiBrand(CarBrand audiBrand) {
    		this.audiBrand = audiBrand;
    	public CarBrand getFordBrand() {
    		return fordBrand;
    	public void setFordBrand(CarBrand fordBrand) {
    		this.fordBrand = fordBrand;
    	public static void main(String[] args) {
    	   	// Initiate Spring application context, this line code will invoke bean initialize method.
    		ApplicationContext springAppContext = new ClassPathXmlApplicationContext("BeanSettings.xml");
    		// Get the car factory bean.
    		CarFactory carFactory = (CarFactory)springAppContext.getBean("carFactoryBean");

4. Use util:constant to declare beans.

  1. The util:constant tag’s static-field attribute value is just the CarBrand class’s final static field AUDI_BRAND or FORD_BRAND.
    <util:constant id="audiBrand" static-field="com.dev2qa.example.spring.bean.finalstatic.CarBrand.AUDI_BRAND" /> 
    <util:constant id="fordBrand" static-field="com.dev2qa.example.spring.bean.finalstatic.CarBrand.FORD_BRAND" /> 
    <bean id="carFactoryBean" class="com.dev2qa.example.spring.bean.finalstatic.CarFactory"> 
          <property name="audiBrand" ref="audiBrand"></property> 
          <property name="fordBrand" ref="fordBrand"></property> 

5. Output.

  1. Below is the above example execution output.
    Brand Name : Audi , Brand Country : Germany
    Brand Name : Ford , Brand Country : United States

6. Questions & Answers.


Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.