Tuesday 19 March 2013

Contribution to SpringFramework


Finally, SpringFramework 3.2.2 is out and includes few very interesting addons:
  • ObjectToStringHttpMessageConverter (SPR-9738) provides a bridge to ConversionService implementations allowing your WebMethods to return numbers, arrays – anything that ConversionService can turn into String. Just register it in your Web context
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.ObjectToStringHttpMessageConverter">
                <constructor-arg>
                    <bean class="org.springframework.context.support.ConversionServiceFactoryBean" />
                </constructor-arg>
                <property name="writeAcceptCharset" value="false" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    and now this is possible:
    @ResponseBody
    public int submitJob() { ... }
  • Jackson2ObjectMapperFactoryBean (SPR-9739) allows you to configure Jackson e.g. in your Web context:
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
                        p:autoDetectFields="false"
                        p:failOnEmptyBeans="false"
                        p:indentOutput="true">
                        <property name="featuresToDisable">
                            <array>
                                <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
                            </array>
                        </property>
                        <property name="serializers">
                            <array>
                                <bean class="org.myproject.NumberSerializer" />
                            </array>
                        </property>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    In majority of cases the configuration will be much simpler – this one shows it with full power.
  • ResourceBundleMessageSource (more exactly: all descendants of AbstractMessageSource) now has commonMessages property which can hold locale-independent values. For example while you want to have mail subject and body locale-dependant, some properties (mail from and mail to) are common across all bundles (no need to duplicate them) (check SPR-10291):
    <bean id="mailProperties" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="org.mycompany.email" />
        <property name="commonMessages">
            <props>
                <prop key="email.from">empty@mydomain.org</prop>
                <prop key="email.to">%s@mydomain.org</prop>
            </props>
        </property>
    </bean>
  • And last but not least important is the introduction of logic that infers type of returned bean (created by some factory) based on the passed arguments (see SPR-9493). That makes e.g. testing much easier to configure (refer SPR-9130) as one need to declare just one factory for all types instead of factory-per-type.
I hope that my small contribution will help many. And separate thanks for code reviewers and commiters of SpringSource.