35

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 ?

3 Answers 3

50

@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.
3
  • 21
    It is important to remember that 3rd point. Do not annotate any @Configuration classes with @EnableWebMvc. Otherwise, Spring MVC will load and use its own serialization/deserialization configuration, ignoring your Spring Boot config.
    – Jack Straw
    Mar 21, 2019 at 23:19
  • 2
    If you don't use @EnableWebMvc annotation you might not initially notice any difference but things like content-type and accept header, generally content negotiation won't work. Jun 26, 2019 at 4:09
  • ~5 hours to understand that also if the web application was working, I didn't had spring-autoconfigure-web in the pom.xml, so no @EnableWebMvc, and so just the MultipartFile wasn't working of the entire web related stuffs, thank you autoconfiguration to implement this easy configuration hard debugging workflow.
    – rascio
    Mar 29, 2021 at 15:55
7

The behavior you get : "all is working" is expected with Spring Boot.
Spring Boot is not Spring : this goes further than Spring.
Indeed, Spring Boot reduces as much as possible the required configuration to allow your application to work.
The @SpringBootApplication annotation that was introduce to make your application a Spring powered application is a good example.
Besides, Spring Boot proposes some starters to package dependencies but also Spring configurations.

In your case, as you declared spring-boot-starter-web as a dependency, the Spring MVC configuration and other things related to web applications with Spring are set.
The documentation states indeed :

11.3.2 The @EnableAutoConfiguration Annotation

Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.

0

Because your using boot application , @SpringBootApplication this annotation by default enable annotation driven application ( mvc:annotation- driven). You do not need to provide the configuration. Read about @SpringBootApplication https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html.

2
  • " You do not need to provide the configuration" means no config file as well ? Jun 24, 2018 at 9:13
  • 1
    means it provide the default basic configuration according to jar in class path and some other way . Not all complex config Jun 24, 2018 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.