XML-based component declaration and injection of Spring Util resources


GitHub: https://github.com/SylkeWay/synthful

XSDs  required by Spring context configuration for Spring util resources.

Employment of Spring util:* resources is demonstrated with the Spring MVC context configuration file /durian/src/main/resources/springweb/mvc-context.xml
<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.xsd
    
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
            
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    ">

An extra XSD schema is required by the context configuration files.
xmlns:util="http://www.springframework.org/schema/util"

And its schema-location mapping appended to the schemaLocation declaration:
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd

util:constant
util:constant is for exposing a static object that has been defined in an existent object instance, as a Spring bean.

  <util:constant id="season"
    static-field="com.synthful.angst.mvc.durian.DurianMVController.SEASON" />
    
  <util:constant id="NULL"
    static-field="com.synthful.angst.model.Address.NULL" />

SEASON is already defined as a static field in DurianMVController class.
@Controller
public class DurianMVController
extends AAngsta{

    final static public String SEASON = "Spring";

NULL is already defined as a static field in Address class.
public class Address
extends AAngsta {
    final static public String NULL = null;

The bean exposed as util:constant can be injected. as is done in class DurianGroveMVController. util:map
util:map is a way to create a Map, as a bean that can be injected as a resource.
  <util:map id="zipMap" key-type="java.lang.Integer" value-type="com.synthful.angst.model.Address">
    <entry key="1001">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg index="0" ref="NULL"/>
        <constructor-arg index="1" value="Hampden"/>
        <constructor-arg index="2" type="int" value="1001"/>
      </bean>
    </entry>
    <entry key="1004">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg name="street" ref="NULL"/>
        <constructor-arg name="city" value="Amhearst"/>
        <constructor-arg name="zip" type="int" value="1004"/>
      </bean>
    </entry>
    <entry key="3910">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg name="street" ref="NULL"/>
        <constructor-arg name="city" value="York Beach"/>
        <constructor-arg name="zip" type="int" value="3910"/>
      </bean>
    </entry>
    <entry key="4444">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg name="street" ref="NULL"/>
        <constructor-arg name="city" value="Hampden"/>
        <constructor-arg name="zip" type="int" value="4444"/>
      </bean>
    </entry>
    <entry key="63440">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg name="street" ref="NULL"/>
        <constructor-arg name="city" value="La Grange"/>
        <constructor-arg name="zip" type="int" value="63440"/>
      </bean>
    </entry>
    <entry key="97101">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg name="street" ref="NULL"/>
        <constructor-arg name="city" value="Amity"/>
        <constructor-arg name="zip" type="int" value="97101"/>
      </bean>
    </entry>
    <entry key="98424">
      <bean class="com.synthful.angst.model.Address">
        <constructor-arg name="street" ref="NULL"/>
        <constructor-arg name="city" value="Tacoma"/>
        <constructor-arg name="zip" type="int" value="98424"/>
      </bean>
    </entry>
  </util:map>

The util:constant bean is injected with @Inject annotation But the util:map bean is injected with @Resource annotation
@Controller
@RequestMapping("/duriangrove")
public class DurianGroveMVController
extends AAngsta{
    
    @Inject private String season; // Inject static field exposed by util:constant
    @Resource (name="zipMap")
    private Map zipMap;



No comments:

Post a Comment