gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:54:56 24 4
gpt4 key购买 nike

所以我写了一个小应用程序,
为了熟悉基础知识,我使它尽可能简单。
我用 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>

我的配置文件只有一个 View 解析器:
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);
}
}

最后是 Controller 类:
包 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@controller工作,但我的应用程序完全正常。
它如何不抛出错误?

最佳答案

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

  • @Configuration 将类标记为 bean 定义的来源
    应用上下文。
  • @EnableAutoConfiguration 告诉 Spring Boot 开始添加 bean
    基于类路径设置、其他 bean 和各种属性
    设置。
  • 通常你会为 Spring MVC 应用程序添加 @EnableWebMvc,但是 Spring
    Boot 在看到 spring-webmvc 时会自动添加
    类路径。这会将应用程序标记为 Web 应用程序,并且
    激活关键行为,例如设置 DispatcherServlet。
  • @ComponentScan 告诉 Spring 寻找其他组件,
    hello 包中的配置和服务,允许它
    找到 Controller 。
  • 关于spring-mvc - 为什么spring-boot应用程序不需要@EnableWebMvc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51008382/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com