
Understanding the web application context configuration
The web application context configuration file (WebApplicationContextConfig.java
) is nothing but a simple Java-based Spring bean configuration class. Spring will create beans (objects) for every bean definition mentioned in this class during the boot up of our application. If you open this web application context configuration file, you will find the following annotations on top of the class definition:
@Configuration
: This indicates that a class declares one or more@Bean
methods@EnableWebMvc
: Adding this annotation to an@Configuration
class imports some special Spring MVC configuration@ComponentScan
: This specifies the base packages to scan for annotated components (beans)
The first annotation @Configuration
indicates that this class declares one or more @Bean
methods. If you remember, in the last section, I explained how we created a bean definition for InternalResourceViewResolver
.
The second annotation is @EnableWebMvc
. With this annotation, we are telling Spring MVC to configure the DefaultAnnotationHandlerMapping
, AnnotationMethodHandlerAdapter
and ExceptionHandlerExceptionResolver
beans. These beans are required for Spring MVC to dispatch requests to the controllers.
Actually, @EnableWebMvc
does many things behind the screen. It also enables support for various convenient annotations such as @NumberFormat
, @DateTimeFormat
to format the form bean's fields during form binding, and similarly the @Valid
annotation to validate the controller method's parameters. It even supports Java objects being converted to/from XML
or JSON
via the @RequestBody
and @ResponseBody
annotation in the @RequestMapping
or @ExceptionHandler
methods during form binding. We will see the usage of these annotations in later chapters. As for now, just remember that the @EnableWebMvc
annotation is needed to enable annotations such as @controller
and @RequestMapping
and so on.
Now the third annotation @ComponentScan
-what is the purpose of this annotation? You need a little bit of background information to understand the purpose of the @ComponentScan
annotation. The @Controller
annotation indicates that a particular class serves the role of a controller. You have already learned that the Dispatcher servlet searches such annotated classes for mapped methods (@RequestMapping
annotated methods) to serve a web request. In order to make the controller available for searching, we must create a bean for that controller in our web application context.
We can create beans for controllers explicitly via the bean configuration (using the @Bean
annotation; you can see how we created a bean for the InternalResourceViewResolver
class using the @Bean
annotation for reference), or we can hand over that task to Spring via an auto-detection mechanism. To enable auto-detection of the @Controller
annotated classes, we must add component scanning to our configuration using the @ComponentScan
annotation. Now you understand the purpose of the @ComponentScan
annotation.
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 @ComponentScan
annotation indicates under which package Spring should search for controller classes to create beans:
@ComponentScan("com.packt.webstore")
This line instructs Spring to search for controller classes in the com.packt.webstore
package and its sub-packages.
Tip
The @ComponentScan
annotation not only recognizes controller classes, it will also recognize other stereotypes such as services and repositories classes as well. We will explore services and repositories later.
Pop quiz - web application context configuration
In order to identify a class as a controller by Spring, what needs to be done?
- That particular class should have an
@Controller
annotation. The @EnableWebMvc
annotation and@ComponentScan
annotation should be specified in the web application context configuration file.- That particular class should be put up in a package or in a sub-package that has been specified as a
value
in the@ComponentScan
annotation. - All of the above.