This page last changed on Oct 12, 2009 by bluk.

Spring Integration

Apache Wink provides an additional module deployed as an external jar in order to provide Spring integration. The Spring integration provides the following features:

  • The ability to register resources and providers from the Spring context, registered as classes or as Spring beans
  • The ability to define the lifecycle of resources or providers that are registered as Spring beans, overriding the default scope specified by the JAX-RS specification.
  • Resources and providers can benefit from Spring features such as IoC and post-processors.
  • Customize Apache Wink from the Spring context. When working with Spring, Apache Wink defines a core spring context that contains customization hooks, enabling easy customization that would otherwise require coding.

Spring Registration

Spring makes it convenient to register resources and providers as spring beans.
Spring Context Loading. In order to load the Spring Context, it is necessary to add a Context Load Listener definition to the web.xml file. The contextConfigLocation context-param must specify the location of the Apache Wink core context file and the application context file, as described in the following example:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:META-INF/server/winkCoreContext-server.xml
               classpath:mycontext.xml
  </param-value>
</context-param>
<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

Registering Resources and Providers

Apache Wink provides the org.apache.wink.spring.Registrar class in order to register resources and providers through a Spring context. The Registrar class extends the WinkApplication class and must be registered as a singleton spring bean. It is possible to define multiple registrars in the same context. All registrars are automatically collected by the runtime and registered as WinkApplication objects during the context loading. The registrar provides the following properties:

  • instances - instances of resources and providers. Ordinarily, these instances are Spring beans, so they can benefit from IoC and other Spring features.
  • classes - a set of resources and providers class names. This property is similar to the getClasses() method of the Application class.
  • priority - the priority of the WinkApplication
Reference
For more information on Priorities refer to section 5.1 Registration and Configuration.
<bean class="org.apache.wink.spring.Registrar">
  <property name="classes">
    <set value-type="java.lang.Class">
      <value>package.className</value>
    </set>
  </property>
  <property name="instances">
    <set>
      <ref bean="resources.resource1" />
      <ref bean="resources.resource2" />
      <ref bean="providers.provider1" />
    </set>
  </property>
</bean>

Custom Properties File Definition

Apache Wink provides a set of customizable properties. When working with Spring, the user should redefine the custom properties file using the Spring context:

<bean id="customPropertiesFactory"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      <value>WEB-INF/configuration.properties</value>
    </list>
  </property>
</bean>
<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="ignoreUnresolvablePlaceholders" value="true" />
  <property name="order" value="1" />
  <property name="propertiesArray">
    <list>
      <props>
       <prop key="wink.propertiesFactory">customPropertiesFactory</prop>
      </props>
    </list>
  </property>
</bean>
  • The customPropertiesFactory bean loads the properties file.
  • The customConfigurer bean overrides the default factory with a custom factory.
  • The order is set to "1". This makes the customConfigurer bean run before the default Apache Wink configurer.
  • In addition, notice that ignoreUnresolvablePlaceholders must be set to true, otherwise the configurer will fail, since some unresolved properties can remain in the context.

Customizing Media-Type Mappings

Apache Wink provides the ability to customize the Media-Type mappings using Spring context.

Reference
For more information on Media-Type Mapping refer to section 5.1 Registration and Configuration .
<bean id="custom.MediaTypeMapper" class="org.apache.wink.server.internal.MediaTypeMapper">
  <property name="mappings">
    <list>
      <map>
        <entry key="userAgentStartsWith" value="Mozilla/" />
        <entry key="resultMediaType">
          <util:constant static-field=" javax.ws.rs.core.MediaType.ATOM" />
        </entry>
        <entry key="typeToSend">
          <util:constant static-field="javax.ws.rs.core.MediaType.TEXT_XML" />
        </entry>
      </map>
    </list>
  </property>
</bean>
<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="ignoreUnresolvablePlaceholders" value="true" />
  <property name="order" value="1" />
  <property name="propertiesArray">
    <list>
      <props>
        <prop key="wink.MediaTypeMapper">custom.MediaTypeMapper</prop>
      </props>
    </list>
  </property>
</bean>
  • The custom.MediaTypeMapper bean creates a new Media-Type mapper.
  • The customConfigurer bean overrides the default factory with a custom factory.

customConfigurer
The order is set to "1". This makes the customConfigurer run before the default Apache Wink configurer.
* In addition, notice that ignoreUnresolvablePlaceholders must be set to true, otherwise the configurer will fail, since some unresolved properties can remain in the context.

Customizing Alternative Shortcuts

Apache Wink provides the ability to customize the Alternative Shortcuts in one of two ways.

Reference
For more information on Alternative Shortcuts Mappings refer to section 5.1 Registration and Configuration.

External Properties File

The shortcuts are defined in a properties file. The shortcuts properties file is loaded in the same way that the configuration properties file is loaded.

<bean id="custom.Shortcuts"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      <value>WEB-INF/shortcuts</value>
    </list>
  </property>
</bean>
<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="ignoreUnresolvablePlaceholders" value="true" />
  <property name="order" value="1" />
  <property name="propertiesArray">
    <list>
      <props>
        <prop key="wink.alternateShortcutsMap">custom.Shortcuts</prop>
      </props>
    </list>
  </property>
</bean>

Spring Context File

Defines the map of the shortcuts in the Spring context.

Document generated by Confluence on Nov 11, 2009 06:57