【WEB系列】Spring MVC之基于java config无xml配置的web应用构建

文章目录
  1. I. Web构建
    1. 1. 项目依赖
    2. 2. 项目结构
    3. 3. 配置设定
      1. a. DispatchServlet 声明
      2. b. java config
    4. 4. 实例代码
    5. 5. 测试
    6. 6. 小结
  2. II. 其他
    1. - 系列博文
    2. 0. 项目
    3. 1. 一灰灰Blog
    4. 2. 声明
    5. 3. 扫描关注

前一篇博文讲了SpringMVC+web.xml的方式创建web应用,用过SpringBoot的童鞋都知道,早就没有xml什么事情了,其实Spring 3+, Servlet 3+的版本,就已经支持java config,不用再写xml;本篇将介绍下,如何利用java config取代xml配置

本篇博文,建议和上一篇对比看,贴出上一篇地址

I. Web构建

1. 项目依赖

对于依赖这一块,和前面一样,不同的在于java config 取代 xml

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<artifactId>200-mvc-annotation</artifactId>
<packaging>war</packaging>

<properties>
<spring.version>5.1.5.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>9.2.19.v20160908</version>
</dependency>
</dependencies>

<build>
<finalName>web-mvc</finalName>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.12.RC2</version>
<configuration>
<httpConnector>
<port>8080</port>
</httpConnector>
</configuration>
</plugin>
</plugins>
</build>

细心的童鞋会看到,依赖中多了一个jetty-all,后面测试篇幅会说到用法

2. 项目结构

第二节依然放上项目结构,在这里把xml的结构也截进来了,对于我们的示例demo而言,最大的区别就是没有了webapp,更没有webapp下面的几个xml配置文件

项目结构

3. 配置设定

现在没有了配置文件,我们的配置还是得有,不然web容器(如tomcat)怎么找到DispatchServlet呢

a. DispatchServlet 声明

同样我们需要干的第一件事情及时声明DispatchServlet,并设置它的应用上下文;可以怎么用呢?从官方找到教程

The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml. In turn, the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling

上面的解释,就是说下面的代码和web.xml的效果是一样一样的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MyWebApplicationInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletCxt) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();

// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletCxt.addServlet("mvc-dispatcher", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/*");
}
}

当然直接实现接口的方式有点粗暴,但是好理解,上面的代码和我们前面的web.xml效果一样,创建了一个DispatchServlet, 并且绑定了url命中规则;设置了应用上下文AnnotationConfigWebApplicationContext

这个上下文,和我们前面的配置文件mvc-dispatcher-servlet有点像了;如果有兴趣看到项目源码的同学,会发现用的不是上面这个方式,而是及基础接口AbstractDispatcherServletInitializer

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
public class MyWebApplicationInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}

@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
// applicationContext.setConfigLocation("com.git.hui.spring");
applicationContext.register(RootConfig.class);
applicationContext.register(WebConfig.class);
return applicationContext;
}

@Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}

@Override
protected Filter[] getServletFilters() {
return new Filter[]{new HiddenHttpMethodFilter(), new CharacterEncodingFilter()};
}
}

看到上面这段代码,这个感觉就和xml的方式更像了,比如Servlet应用上下文和根应用上下文

说明

上面代码中增加的Filter先无视,后续会有专文讲什么是Filter以及Filter可以怎么用

b. java config

前面定义了DispatchServlet,接下来对比web.xml就是需要配置扫描并注册bean了,本文基于JavaConfig的方式,则主要是借助 @Configuration 注解来声明配置类(这个可以等同于一个xml文件)

前面的代码也可以看到,上下文中注册了两个Config类

RootConfig定义如下,注意下注解@ComponentScan,这个等同于<context:component-sca/>,指定了扫描并注册激活的bean的包路径

1
2
3
4
@Configuration
@ComponentScan(value = "com.git.hui.spring")
public class RootConfig {
}

另外一个WebConfig的作用则主要在于开启WebMVC

1
2
3
4
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
}

4. 实例代码

实例和上一篇一样,一个普通的Server Bean和一个Controller

1
2
3
4
5
6
@Component
public class PrintServer {
public void print() {
System.out.println(System.currentTimeMillis());
}
}

一个提供rest服务的HelloRest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
public class HelloRest {
@Autowired
private PrintServer printServer;

@GetMapping(path = "hello", produces="text/html;charset=UTF-8")
public String sayHello(HttpServletRequest request) {
printServer.print();
return "hello, " + request.getParameter("name");
}


@GetMapping({"/", ""})
public String index() {
return UUID.randomUUID().toString();
}
}

5. 测试

测试依然可以和前面一样,使用jetty来启动,此外,介绍另外一种测试方式,也是jetty,但是不同的是我们直接写main方法来启动服务

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
public class SpringApplication {

public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletContextHandler handler = new ServletContextHandler();

// 服务器根目录,类似于tomcat部署的项目。 完整的访问路径为ip:port/contextPath/realRequestMapping
//ip:port/项目路径/api请求路径
handler.setContextPath("/");

AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(WebConfig.class);
applicationContext.register(RootConfig.class);

//相当于web.xml中配置的ContextLoaderListener
handler.addEventListener(new ContextLoaderListener(applicationContext));

//springmvc拦截规则 相当于web.xml中配置的DispatcherServlet
handler.addServlet(new ServletHolder(new DispatcherServlet(applicationContext)), "/*");

server.setHandler(handler);
server.start();
server.join();
}
}

测试示意图如下

测试示意图

6. 小结

简单对比下xml的方式,会发现java config方式会清爽很多,不需要多个xml配置文件,维持几个配置类,加几个注解即可;当然再后面的SpringBoot就更简单了,几个注解了事,连上面的两个Config文件, ServletConfig都可以省略掉

另外一个需要注意的点就是java config的运行方式,在servlet3之后才支持的,也就是说如果用比较老的jetty是起不来的(或者无法正常访问web服务)

II. 其他

- 系列博文

web系列:

mvc应用搭建篇:

0. 项目

1. 一灰灰Blog

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

2. 声明

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

3. 扫描关注

一灰灰blog

QrCode

知识星球

goals


打赏 如果觉得我的文章对您有帮助,请随意打赏。
分享到