Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Springframework P-notation was made to make creating spring config files a little neater and shorter. Here is what a typical file would look like.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="com.cdi.rng"/>
    <mvc:annotation-driven/>

    <util:constant id="mediaTypeJson" static-field="org.springframework.http.MediaType.APPLICATION_JSON"/>

    <bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
    </bean>
    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="defaultContentType" ref="mediaTypeJson"/>
    </bean>

</beans>

Here is what it looks like once the P-notation is used.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       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/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util.xsd">

    <context:component-scan base-package="com.cdi.rng"/>
    <mvc:annotation-driven/>
    <util:constant id="mediaTypeJson" static-field="org.springframework.http.MediaType.APPLICATION_JSON"/>

    <bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
          p:contentNegotiationManager-ref="contentNegotiationManager"/>
    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"
          p:defaultContentType-ref="mediaTypeJson"/>

</beans>

Springframework P-notation does not add capability, but does reduce the number of characters needed to define the Spring context.

December 8th, 2014

Posted In: Spring

Tags: , , , , , ,

One Comment

A friend of mine had to use this and I thought it was something worth remembering for the Springframework.

    <util:list id="tmDateFormats" >
        <value>yyyy-MM-dd</value>
        <value>dd-MMM-yy</value>
        <value>MM/dd/yyyy</value>
    </util:list>

He injected them with SpEl

    @Resource(name="tmDateFormats")
    private List<String> dateFormats;

February 6th, 2014

Posted In: Java, Spring

Tags: , ,

Leave a Comment

WP to LinkedIn Auto Publish Powered By : XYZScripts.com