Spring P-notation

Spring 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>
This entry was posted in Spring and tagged , , , , , , . Bookmark the permalink.

1 Response to Spring P-notation

  1. Pingback: Spring C-notation | Java Ninja Chronicles By Norris Shelton, Jr

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s