- Spring MVC Beginner’s Guide
- Amuthan G
- 524字
- 2021-07-16 11:25:44
The web application context configuration
The web application context configuration file (DispatcherServlet-context.xml
) is nothing but a simple Spring bean configuration file. Spring will create beans (objects) for every bean definition mentioned in this file during bootup of our application. If you open this web application context configuration file (/WEB-INF/spring/webcontext/DispatcherServlet-context.xml
), you will find some configuration and bean definition as follows:
<?xml version="1.0" encoding="UTF-8"?> <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" 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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.packt.webstore" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
The first tag within the <beans>
definition is <mvc:annotation-driven />
. By this tag, we tell Spring MVC to configure the DefaultAnnotationHandlerMapping
, AnnotationMethodHandlerAdapter
, and ExceptionHandlerExceptionResolver
beans. These beans are required for Spring MVC to dispatch requests to the controllers.
Actually <mvc:annotation-driven />
does many things behind the screen. It also enables support for various convenient annotations such as @NumberFormat
and @DateTimeFormat
to format the form bean fields during form binding. Similarly, we have the @Valid
annotation to validate the controller method's parameters. It also supports Java objects to/from an XML or JSON conversion via the @RequestBody
and @ResponseBody
annotations in the @RequestMapping
or @ExceptionHandler
method during form binding. We will see the usage of these annotations in later chapters. As of now, just remember that the <mvc:annotation-driven />
tag is needed to enable annotations such as @controller
and @RequestMapping
.
What is the purpose of the second tag, <context:component-scan>
? You need a bit of background information to understand the purpose of the <context:component-scan>
tag. The @Controller
annotation indicates that a particular class serves the role of a controller. We already learned that the dispatcher servlet searches such annotated classes for mapped methods (the @RequestMapping
annotated methods) to serve a web request. In order to make the controller available for searching, we need to create a bean for this controller in our web application context.
We can create beans for controllers explicitly via the bean configuration (using the <bean>
tag—you can see how we created a bean for the InternalResourceViewResolver
class using the <bean>
tag in the next section), or we can hand over that task to Spring via the autodetection mechanism. To enable the autodetection of the @Controller
annotated classes, we need to add component scanning to our configuration using the <context:component-scan>
tag. Now, you finally understand the purpose of the <context:component-scan>
tag.
Spring will create beans (objects) for every @Controller
class at runtime. The dispatcher servlet will search for the correct request mapping method in every @Controller
bean based on the @RequestMapping
annotation, to serve a web request. The base-package
property of a <context:component-scan>
tag indicates the package under which Spring should search for controller classes to create beans:
<context:component-scan base-package="com.packt.webstore" />
The preceding line instructs Spring to search for controller classes within the com.packt.webstore
package and its subpackages.
Pop quiz – web application context configuration
Q1. What needs to be done to identify a class by Spring as a controller?
- That particular class should have the
@Controller
annotation. - The
<mvc:annotation-driven />
and<context:component-scan>
tags should be specified in the web application context configuration file. - That particular class should be put up in a package or subpackage that has been specified as a base package in the
<context:component-scan>
tag. - All of the above.