Friday, February 19, 2016

How to use static fields within Spring XML Configuration

When configuring Spring through XML, you can use static fields in a similar way you use instances defined in the same XML or elsewhere. This is possible by using the util namespace as highlighted below.

<?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:context="http://www.springframework.org/schema/context"
    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 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

  <bean id="myObject" class="com.javacreed.examples.howto.spring.StaticInXml">
    <constructor-arg>
      <list>
        <util:constant static-field="com.javacreed.examples.howto.spring.MyFields.OBJECT_1" />
      </list>
    </constructor-arg>
  </bean>

</beans>

No comments: