使用Java类代替web-xml
# 使用 Java 类代替 web.xml 启动 Spring MVC
在 servlet3.0 当中支持使用 Java 的类来配置启动容器,使用方法如下面的 Java 代码所示,可以作为模板使用:
package top.dimples.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
/**
* @author dimples_yj
* @since 2020-09-09
* 当容器启动时,会调用该类中的onStartup方法
*/
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// 获取SpringMVC的上下文环境
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
// 将SpringMVC的配置文件加载到上下文环境
ctx.register(WebMvcConfig.class);
// 设置servletContext上下文环境
ctx.setServletContext(servletContext);
// 设置SpringMVC的转发器
DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcherServlet",dispatcherServlet);
// 设置路径
servlet.addMapping("/");
// 设置启动级别
servlet.setLoadOnStartup(1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
代码当中的WebMvcConfig文件
package top.dimples.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* @author dimples_yj
* @since 2020-09-09
*/
@Configuration
@EnableWebMvc
@ComponentScan("top.dimples.controller")
public class WebMvcConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上述代码引入的各个 jar 包如下所示,使用 Gradle 作为构建工具
plugins {
id 'java'
id 'war'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework', name: 'spring-context', version: '5.2.6.RELEASE'
compile group: 'org.springframework', name: 'spring-web', version: '5.2.6.RELEASE'
compile group: 'org.springframework', name: 'spring-webmvc', version: '5.2.6.RELEASE'
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.11.1'
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
帮助我修改此页面 (opens new window)
上次更新: 2022/09/12, 15:00:20