servlet-context.xml
The various components from the above xmls are described as follows:
<annotation-driven />:
- Initialiazes components required for dispatching the requests to our Controllers.
- Register's a RequestMappingHandlerMapping, a RequestMappingHandlerAdapter, and an ExceptionHandlerExceptionResolver in support of processing requests with annotated controller methods using annotations such as
@RequestMapping, @ExceptionHandler
and others. - Configures support for new Spring MVC features such as declarative validation with
@Valid, HTTP message conversion with @RequestBody/@ResponseBody, formatting Number fields with @NumberFormat
etc.
This element should be placed in your DispatcherServlet context (or in your root context if you have no DispatcherServlet context defined)
<context:component-scan>:
- Scans for the classes annotated with
@Component, @Service, @Repository and @Controller
defined under the base-package and registers their corresponding beans. @Component
serves as a generic stereotype for any Spring-managed component; whereas,@Repository, @Service, and @Controller
serve as specializations of @Component for more specific use cases.
<resources>:
This tag allows static resource requests following a particular URL pattern to be served by a ResourceHttpRequestHandler from any of a list of Resource location.
This provides a convenient way to serve static resources from location other than the web application root, including locations on the classpath
e.g. <mvc:resources mapping="/resources/**" location="/, classpath:/web-resources/"/>
InternalResourceViewResolver:
- Spring provides different view resolvers to render models in a browser without tying you with a specific view technology.
- Spring enables you to use JSPs, Velocity templates and XSLT views.
InternalResourceViewResolver is one of them.
src/main/webapp/WEB-INF/sping/servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="org.postgresql.Driver" />
<beans:property name="url"
value="jdbc:postgresql://localhost:5432/phones" />
<beans:property name="username" value="postgres" />
<beans:property name="password" value="1234" />
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="annotatedClasses">
<beans:list>
<beans:value>com.spring.demo.model.Phone</beans:value>
</beans:list>
</beans:property>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<beans:bean id="phoneDAO" class="com.spring.demo.dao.PhoneDAOImpl">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
<beans:bean id="phoneService" class="com.spring.demo.service.PhoneServiceImpl">
<beans:property name="phoneDAO" ref="phoneDAO"></beans:property>
</beans:bean>
<context:component-scan base-package="com.spring.demo" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
</beans:beans>