springwebmvc简化之springboot :
1.1、完全自动装配
spirngwebmvc | springbooot | |
---|---|---|
自动装配 | DispatcherServlet | DispatcherServletAutoConfiguration |
替换 | @EnableWebMvc | WebMvcAutoConfiguration |
Servlet 容器 | : | ServletWebServerFactoryAutoConfiguration |
1.1.2、自动配置顺序性:
绝对顺序 | @AutoConfigureOrder |
相对顺序 | @AutoConfigureAfter |
1.2、装配条件
1)Web 类型判断( ConditionalOnWebApplication
)
WebApplicationType | |
---|---|
SERVLET 类型 | WebApplicationType.SERVLET |
2)API 判断( @ConditionalOnClass
)
Servlet |
---|
Servlet |
Spring Web MVC |
---|
DispatcherServlet |
WebMvcConfigurer |
如:
package org.springframework.boot.autoconfigure.web.servlet;
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
// ... 省略
public class WebMvcAutoConfiguration {
// ... 省略
}
3)Bean 判断()
当Bean不存在时 | 当Bean存在时 |
---|---|
@ConditionalOnMissingBean | @ConditionalOnBean |
如:
当不存在WebMvcConfigurationSupport
这个Bean时,才装配WebMvcAutoConfiguration
这个类。
package org.springframework.boot.autoconfigure.web.servlet;
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
// ... 省略
public class WebMvcAutoConfiguration {
// ... 省略
}
1.3、外部化配置
1)Web MVC 配置
WebMvcProperties
2)资源配置
ResourceProperties
Spring Boot JSP 依赖:
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
Demo:
springboot-webmvc
├── pom.xml
└── src
└── main
├── java
└── com
│ └── example
│ └── web
│ ├── HomeController.java
│ ├── boostrap
│ │ └── SpringBootWebMvcBootstrap.java
│ └── config
│ └── WebMvcConfig.java
│
├── resources
│ └── application.properties
└── webapp
└── WEB-INF
└── jsp
└── index.jsp
SpringBootWebMvcBootstrap
:
package com.example.web.boostrap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringBootWebMvc 引导类
*/
@SpringBootApplication(scanBasePackages = "com.example.web")
public class SpringBootWebMvcBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebMvcBootstrap.class, args);
}
}
WebMvcConfig
:
package com.example.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Spring web mvc 配置类
*/
@Configuration
//@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
// @Bean
// public ViewResolver viewResolver() {
// InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
// viewResolver.setPrefix("/WEB-INF/jsp/");
// viewResolver.setSuffix(".jsp");
// viewResolver.setViewClass(JstlView.class);
// return viewResolver;
// }
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截中。。。");
return true;
}
});
}
}
application.properties
:
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp
HomeController
:
package com.example.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Home {@link Controller}
*
*/
@Controller
public class HomeController {
@RequestMapping("")
public String index() {
return "index";
}
}
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">
<parent>
<artifactId>springbootlearn</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-webmvc</artifactId>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>springboot</docker.image.prefix>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
打包运行:
mvn -Dmaven.test.skip -U clean package
java -jar target/springboot-webmvc-0.0.1-SNAPSHOT.war
在浏览器输入localhost:8080即可。