Gag*_*ngh 9 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-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>
Run Code Online (Sandbox Code Playgroud)
我的配置文件只有一个视图解析器:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
主文件
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);
}
}
Run Code Online (Sandbox Code Playgroud)
最后是控制器类: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";
}
}
Run Code Online (Sandbox Code Playgroud)
Application.properties
server.servlet.context-path=/demo
Run Code Online (Sandbox Code Playgroud)
因此,这就是整个应用程序,正如我记得mvc:annotation- driven在web.xml @enablewebmvc中获取@getmapping或@controller工作所需的那样,但是我的应用程序完全可以工作。如何不抛出错误?
小智 14
@SpringBootApplication是一个方便注释,它添加了以下所有内容:
您得到的行为:Spring Boot应该会“一切正常”。
Spring Boot不是Spring:比Spring更进一步。
实际上,Spring Boot会尽可能减少所需的配置,以使您的应用程序正常工作。为使您的应用程序成为Spring支持的应用程序而引入
的@SpringBootApplication注释就是一个很好的例子。
此外,Spring Boot提出了一些入门工具来打包依赖关系以及Spring配置。
在您的情况下,正如您声明spring-boot-starter-web为依赖项一样,将设置Spring MVC配置以及与Spring的Web应用程序相关的其他内容。
该文档确实指出:
11.3.2 @EnableAutoConfiguration批注
由于spring-boot-starter-web添加了Tomcat和Spring MVC,因此自动配置假定您正在开发Web应用程序并相应地设置Spring。
| 归档时间: |
|
| 查看次数: |
5167 次 |
| 最近记录: |