Spring mvc 为什么spring启动应用程序没有';不需要@EnableWebMvc
所以我写了一个小应用程序, 为了熟悉基础知识,我把它做得尽可能简单。 我用Config.java文件制作了一个简单的mvc应用程序,当我认为现在应用程序应该抛出一个错误时,它实际上可以工作了 这是我的pom.xmlSpring mvc 为什么spring启动应用程序没有';不需要@EnableWebMvc,spring-mvc,spring-boot,Spring Mvc,Spring Boot,所以我写了一个小应用程序, 为了熟悉基础知识,我把它做得尽可能简单。 我用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
<?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.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
这就是整个应用程序,我记得我需要web.xml中的mvc:annotation-driven
,或者@enablewebmvc
来让@getmapping
和@controller
工作,但我的应用程序完全工作。
它怎么不会抛出错误呢?因为您使用的是启动应用程序@springbootplication,默认情况下,此注释启用注释驱动的应用程序(mvc:annotation-driven)。您不需要提供配置。了解@SpringBootApplication 弹簧靴不是弹簧:这比弹簧更进一步。
实际上,Spring Boot尽可能减少了允许应用程序工作所需的配置。
@SpringBootApplication
注释就是一个很好的例子,它使您的应用程序成为一个Spring驱动的应用程序
除此之外,Spring Boot还提供了一些启动包依赖项,以及Spring配置
在您的例子中,正如您声明的springbootstarterweb
作为一个依赖项,spring的MVC配置和其他与spring的web应用程序相关的内容都已设置。各国确实: 11.3.2@EnableAutoConfiguration注释 由于SpringBootStarterWeb添加了Tomcat和SpringMVC 自动配置假定您正在开发web应用程序 并据此设置弹簧
@SpringBootApplication是一个方便的注释,添加了以下所有内容:
- @配置将类标记为类的bean定义源 应用程序上下文
- @EnableAutoConfiguration告诉springboot开始添加bean 基于类路径设置、其他bean和各种属性 设置
- 通常,您会为Spring MVC应用程序添加@EnableWebMvc,但Spring Boot在服务器上看到SpringWebMVC时会自动添加它 类路径。这会将应用程序标记为web应用程序,并 激活关键行为,例如设置DispatcherServlet
- @ComponentScan告诉Spring寻找其他组件, hello包中的配置和服务,允许 找到控制器
@EnableWebMvc
注释任何@配置
类。否则,Spring MVC将加载并使用其自己的序列化/反序列化配置,忽略您的Spring引导配置。如果您不使用@EnableWebMvc注释,您最初可能不会注意到任何差异,但会注意到内容类型和accept标头等内容,一般来说,内容协商是不起作用的。我花了大约5个小时来了解,如果web应用程序正常工作,我在pom.xml中没有spring autoconfigure web
,所以没有@EnableWebMvc,所以只是多部分文件无法处理整个与web相关的内容,感谢您通过自动配置来实现此简单的配置硬调试工作流。
server.servlet.context-path=/demo