一、如何搭建一个SpringMVC
搭建一个SpringMVC有两种方式:
一种是用xml文件进行配置;另一种是用Java代码进行配置
用xml文件搭建SpringMVC
搭建SpringMVC首先要配置DispatcherServlet和ContextLoaderListener,这两个在web.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--该配置项的功能是:ContextLoaderListener会加载contextConfigLocation指定的文件root-context.xml
并将root-context.xml中定义的bean加载到ContextLoaderListener创建的上下文中-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在上面的web.xml文件中,主要分成两块:
- 一块是定义ContextLoaderListener,以及其创建的上下文需要加载的bean是定义在哪个文件中的(即contextConfigLocation所指定的文件)
- 另一块是定义DispatcherServlet,以及DispatcherServlet所要处理的URL路径。那DispatcherServlet创建的上下文所需要加载的bean在哪里定义呢?这里有两种定义方式:
- DispatcherServlet默认会根据Servlet的名字,去找对应的bean配置文件,例如DispatcherServlet的名字为appServlet,那么DispatcherServlet会从“/WEB-INF/appServlet-context.xml”中加载bean至其创建的上下文中。
- 也可以自己指定配置文件,只需要在<Servlet>标签中通过contextConfigLocation指定即可,此时,DispatcherServlet的定义如下:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
完成上面的web.xml的配置,我们还要再分别编写root-context.xml和servlet-context.xml将需要加载的bean分别配置到这两个文件中。
按xml文件配置的方式就先讲到这里,下面着重讲解一下按Java代码进行配置。
按Java代码进行配置搭建SpringMVC
此种方法虽然是按Java代码进行配置,但是还是需要将DispatcherServlet和ContextLoaderListener在web.xml中进行配置,然后在此基础上再加上Java代码配置。
第一步:
在web.xml中配置DispatcherServlet和ContextLoaderListener,此时web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
上述web.xml只是配置了DispatcherServlet和ContextLoaderListener,但此时你还没有告诉DispatcherServlet和ContextLoaderListener去哪里加载bean到它们所创建的上下文中。因为我们要使用Java代码来配置这些bean,而不是使用xml文件来配置,所以这里就和上面的第一种搭建方式不一样了。
既然要去Java代码中寻找要加载的bean,那么就要使用一种Spring上下文AnnotationConfigWebApplicationContext,它是WebApplicationContext的实现类,这个上下文会去加载java配置类,然后将配置类中配置的bean加载到其中。
所以,我们现在要做的是告诉DispatcherServlet和ContextLoaderListener,在创建上下文的时候,创建AnnotationConfigWebApplicationContext这个上下文。那怎么告诉DispatcherServlet和ContextLoaderListener呢?这就需要在web.xml中对DispatcherServlet和ContextLoaderListener进行设置了。
第二步:
在web.xml中对DispatcherServlet和ContextLoaderListener进行初始化设置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!--这个配置项的作用是:告诉ContextLoaderListener在创建上下文时使用AnnotationConfigWebApplicationContext进行创建-->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!--指定AnnotationConfigWebApplicationContext加载bean时所需要的bean的配置类-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.fy.config.RootConfig</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.fy.config.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这个地方对DispatcherServlet和ContextLoaderListener的初始化设置也可以分为两块,一块是ContextLoaderListener,另一块是DispatcherServlet,然后分别在每一块中又有两个部分:一个是上下文容器的配置,另一个是bean的配置类的配置。
- ContextLoaderListener的初始化设置
- 指定ContextLoaderListener创建上下文时,使用AnnotationConfigWebApplicationContext进行创建
- 指定ContextLoaderListener创建的上下文在加载bean时需要找哪个bean配置类进行加载
- DispatcherServlet的初始化设置,其初始化设置跟ContextLoaderListener的初始化设置一样,只不过在web.xml中写的地方不一样
- 指定DispatcherServlet创建上下文时,使用AnnotationConfigWebApplicationContext进行创建
- 指定DispatcherServlet创建的上下文在加载bean时需要找哪个bean配置类进行加载
完成上面的web.xml的配置后就可以编写Java配置类了。