Spring基础篇:JavaWeb与Spring结合的两种方式

93 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第20天,点击查看活动详情

思路

开始了解前,建议先简单了了解一下思想。

核心思路分享:Spring做的一件核心的事就是容器,这个容器管理了很多Bean。而Web工程是Tomcat读取Web.xml的配置去做一些事,在Web工程中有专门的域,比方说ServletContext,这个域在只有Web.xml的情况下是不包含Spring的任何特征的,那学习过Spring之后都觉得Spring香!怎么在ServletConext拿到Spring的配置呢?这就是该篇文章想分享的内容。当然这里只说步骤了,不分析为什么。

webapp目录不存在,不知道web.xml放哪的问题

当然可能不仅仅是Maven工程会遇到的问题,如果你在项目搭建过程中出现了:缺少webapp目录,或者不太清楚web.xml放哪,请往下看。

Web工程的目录一般是这样的(不是说光写后端接口的那种分离工程啊,有可能遇到的工程就不是分离的呢?)

image.png 以我图为例解释的话就是:

  • spring-web :根工程文件
  • src:源码目录
  • main:主程序目录

(默认的基础目录结构就是这样的吧)


  • java:放java类代码的地方,一般会以倒写的域名来命名包名,这里面的Java代码会被编译。
  • resources:存放程序需要使用的资源文件
  • webapp:web工程特有的目录
  • WEB-INF:固定名称,用来存放web.xml配置文件的
  • web.xml:web项目的配置文件

  • test:存放单元测试的目录

解决创建webapp目录,指定web.xml生成位置

以IDEA这个软件为例子:

  1. 依次点击:fileProject StructureMoudule

  2. 点击你自己想添加Web框架的Module工程,点击 + 号,在弹出的下拉框中选择Web

  3. 点击添加完成的Web,在右侧中先编辑Web Resource Directories:这个是指定webapp在哪;然后编辑Deployment Descriptors:这个是指定生成web.xml的位置,根据我们图中了解,web.xml是在webapp目录下的子目录WEB-INF中。

image.png

以下内容还并为是SpringMVC的内容,是JavaWeb与Spring相结合的两种方式。

依赖导入

JavaWeb与Spring结合,需要导入

  1. Servlet的依赖
  2. Spring-context的依赖
  3. Spring-Web依赖(关键)
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

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

SpringMVC搭建方式一:通过web.xml的方式加载容器

web.xml加载容器,这里是指将IOC的容器加载到web.xml中。那么在web.xml中加载IOC配置的方式之一:在web.xml中加载Spring的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
     
    <!--固定写法-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:xxx/spring-web.xml</param-value>
    </context-param>
</web-app>

这里通过监听器,监听Spring容器启动后,在web上下文contextConfigLocation中,指定Spring配置文件的位置。

SpringMVC搭建方式二:通过继承AbstractContextLoaderInitializer的方式

这个写法可以规避写web.xml

  1. 通过注解的方式写Spring配置类
@Configuration
public class ApplicationConfiguration {
    @Bean
    public UserService userService() {
        return new UserService();
    }
}
  1. 然后我们需要做的就是替换掉Web.xml并且加载Spring配置类。
public class DemoWebApplicationInitializer extends AbstractContextLoaderInitializer {
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(ApplicationConfiguration.class);
        return ctx;
    }
}