官术网_书友最值得收藏!

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.

Tip

The <context:component-scan> tag not only recognizes controller classes, it also recognizes other stereotypes such as services and repository classes as well. We will learn more about services and repositories later.

Pop quiz – web application context configuration

Q1. What needs to be done to identify a class by Spring as a controller?

  1. That particular class should have the @Controller annotation.
  2. The <mvc:annotation-driven /> and <context:component-scan> tags should be specified in the web application context configuration file.
  3. 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.
  4. All of the above.
主站蜘蛛池模板: 黄大仙区| 福海县| 双辽市| 高尔夫| 从化市| 五台县| 富裕县| 田林县| 彰化市| 马鞍山市| 个旧市| 巴彦淖尔市| 台南市| 城步| 永清县| 陈巴尔虎旗| 淮阳县| 平远县| 田阳县| 洞口县| 济南市| 密云县| 临西县| 册亨县| 公安县| 桦南县| 靖江市| 墨江| 梓潼县| 内乡县| 天气| 长白| 大新县| 白玉县| 鄂托克前旗| 石渠县| 内乡县| 榆林市| 诏安县| 离岛区| 连城县|