为什么 spring-boot 应用程序不需要 @EnableWebMvc

2024-06-19

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

这是我的 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>

我的配置文件只有一个视图解析器:

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;

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

这就是整个应用程序,我记得我需要mvc:annotation- driven在 web.xml 或@enablewebmvc为了得到@getmapping and @controller可以工作,但我的应用程序完全可以工作。 它如何不抛出错误?


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

  • @Configuration 将类标记为 bean 定义的源 应用程序上下文。
  • @EnableAutoConfiguration告诉Spring Boot开始添加bean 基于类路径设置、其他 bean 和各种属性 设置。
  • 通常你会为 Spring MVC 应用程序添加@EnableWebMvc,但是 Spring Boot 在看到 spring-webmvc 时会自动添加它 类路径。这会将应用程序标记为 Web 应用程序,并且 激活关键行为,例如设置 DispatcherServlet。
  • @ComponentScan 告诉 Spring 寻找其他组件, hello 包中的配置和服务,使其能够 找到控制器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 spring-boot 应用程序不需要 @EnableWebMvc 的相关文章

随机推荐