Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why spring-boot application doesn't require @EnableWebMvc

Tags:

So i wrote a small application, In order to get familiar with basics i made it as simple as possible. I made a simple mvc application with Config.java file and when i thought that now the application should throw an error it actually works.

Here's my pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <modelVersion>4.0.0</modelVersion>      <groupId>com.example</groupId>     <artifactId>demo</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>jar</packaging>      <name>demo</name>     <description>Demo project for Spring Boot</description>      <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-parent</artifactId>         <version>2.0.3.RELEASE</version>         <relativePath/> <!-- lookup parent from repository -->     </parent>      <properties>         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>         <java.version>1.8</java.version>     </properties>      <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter</artifactId>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>          <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-thymeleaf</artifactId>         </dependency>          <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-web</artifactId> </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> 

My Config file which only has a view resolver:

package com.example.demo;  import org.springframework.context.annotation.Bean;  import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver;  import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView;   @Configuration public class DemoConfig {      @Bean     public ViewResolver internalResourceViewResolver() {         InternalResourceViewResolver bean = new InternalResourceViewResolver();         bean.setViewClass(JstlView.class);         bean.setPrefix("/templates/");         bean.setSuffix(".html");         return bean;     } } 

Main file

package com.example.demo;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;  @SpringBootApplication public class DemoApplication {      public static void main(String[] args) {         SpringApplication.run(DemoApplication.class, args);     } } 

And finally the controller class : package com.example.demo.controller;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping;  @Controller public class TestController {     @GetMapping(value="home")     public String home() {         return "home";     } } 

Application.properties

server.servlet.context-path=/demo 

So this is the entire application , as i can recall i require mvc:annotation- driven in web.xml or @enablewebmvc for getting @getmapping and @controller to work but my application works completely . How is it not throwing an error ?

like image 885
Gagan Singh Avatar asked Jun 24 '18 08:06

Gagan Singh


1 Answers

@SpringBootApplication is a convenience annotation that adds all of the following:

  • @Configuration tags the class as a source of bean definitions for the application context.
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
  • @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
like image 148
Gaurav Srivastav Avatar answered Oct 13 '22 09:10

Gaurav Srivastav