为什么 spring-boot 应用程序不需要 @EnableWebMvc [英] why spring-boot application doesn't require @EnableWebMvc

查看:25
本文介绍了为什么 spring-boot 应用程序不需要 @EnableWebMvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一个小应用程序,为了熟悉基础知识,我使它尽可能简单.我用 Config.java 文件制作了一个简单的 mvc 应用程序,当我认为现在应用程序应该抛出错误时,它实际上可以工作.

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.

这是我的 pom.xml

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;
    }
}

主文件

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);
    }
}

最后是控制器类:包com.example.demo.controller;

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";
    }
}

应用程序属性

server.servlet.context-path=/demo

所以这是整个应用程序,因为我记得我需要 web.xml 中的 mvc:annotation-driven@enablewebmvc 来获取 @getmapping@controller 工作,但我的应用程序完全工作.怎么不报错?

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 ?

推荐答案

@SpringBootApplication 是一个方便的注解,添加了以下所有内容:

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

  • @Configuration 将该类标记为应用上下文.
  • @EnableAutoConfiguration 告诉 Spring Boot 开始添加 bean基于类路径设置、其他 bean 和各种属性设置.
  • 通常您会为 Spring MVC 应用程序添加 @EnableWebMvc,但 SpringBoot 看到 spring-webmvc 时会自动添加类路径.这将应用程序标记为 Web 应用程序并且激活关键行为,例如设置 DispatcherServlet.
  • @ComponentScan 告诉 Spring 寻找其他组件,hello 包中的配置和服务,允许它找到控制器.

这篇关于为什么 spring-boot 应用程序不需要 @EnableWebMvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆