Suggested Pages

Monday, May 28, 2012

DispatcherServlet vs ContextLoaderListener

DispatcherServlet


Suggested Pages:


DispatcherServlet loads its configuration file using <servlet_name>-servlet.xml. You can configure a custom config file using contextConfigLocation param inside <init-param> tag of DispatcherServlet .

web.xml

<servlet>
 <servlet-name>addressbook</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/spring-mvc.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>


spring-mvc.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 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/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           ">

<context:annotation-config />
 
 <context:component-scan base-package="com.simonefolinoblogspot.controller" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/pages/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

As you can see in this file there's only a scanning of @Controller annotated beans. This file should contain web tier components as:
  • Controllers
  • ViewResolvers
  • LocaleResolvers
  • ThemeResolvers
These components are not middle tier components.

ContextLoaderListener

ContextLoaderListener loads its configuration file using contextConfigLocation param.
web.xml

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

spring-context.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 xmlns:cxf="http://cxf.apache.org/core"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd   http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd    http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-3.0.xsd    http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   
 <context:annotation-config />

 <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />

 <context:component-scan base-package="com.simonefolinoblogspot.dao" >
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
 </context:component-scan>

     ...

</beans>


As you can see in this file there's not a scanning of @Controller annotated beans because we are interested into other annotated beans such as @Service, @Repository etc.. This file contains your middle tier and data tier components:
  • DAO: such as @Repository bean
  • Entity: such as @Entity bean
  • Service: such as @Service bean
Spring will merge all these components into ApplicationContext. Then the middle tier components is accessible from your web tier components.

No comments :

Post a Comment

Suggested Pages