Spring与SpringMVC整合

来源:互联网 发布:linux session问题 编辑:程序博客网 时间:2024/06/07 21:45

如何创建SpringMVC项目:

http://blog.csdn.net/x_iya/article/details/68945373

渐渐地喜欢上了这种开发方式。

build.gradle

group 'com.xiya'version '1.0-SNAPSHOT'apply plugin: 'idea'apply plugin: 'java'apply plugin: 'war'apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'sourceCompatibility = 1.8repositories {    maven {        url 'http://maven.aliyun.com/nexus/content/groups/public/'    }}dependencies {    compile group: 'org.springframework', name: 'spring-context', version: '4.3.7.RELEASE'    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.7.RELEASE'    providedCompile group: 'javax.servlet', name: 'jsp-api', version: '2.0'}task wrapper(type: Wrapper) {    gradleVersion = '3.0'}tasks.withType(JavaCompile) {    options.encoding = "UTF-8"}

如上,我们已经可以运行一个SpringMVC HelloWorld程序了。

在上面项目的基础上,我们加入Spring。

参考: 在WEB应用中使用spring


Spring与SpringMVC整合的目的是为了能够在SpringMVC的请求处理方法中使用Spring IOC容器里的bean。

web.xml

<?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_3_1.xsd"         version="3.1">    <!-- 配置Spring配置文件的名称和位置 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>    </context-param>    <!-- 启动IOC容器的ServletContextListener -->    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>        <servlet>        <servlet-name>SpringSpringMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:springmvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>        <servlet-mapping>        <servlet-name>SpringSpringMVC</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>
以上目的实现,是通过ContextLoaderListener监听器实现的。

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {   public ContextLoaderListener() {   }   public ContextLoaderListener(WebApplicationContext context) {      super(context);   }   @Override   public void contextInitialized(ServletContextEvent event) {      initWebApplicationContext(event.getServletContext());   }   @Override   public void contextDestroyed(ServletContextEvent event) {      closeWebApplicationContext(event.getServletContext());      ContextCleanupListener.cleanupAttributes(event.getServletContext());   }}

在学习Servlet时,我们知道Servlet容器一加载就会执行contextInitialized方法。

这样web容器已加载,就去初始化Spring的相关配置。

initWebApplicationContext中所做的主要是:

public class ContextLoader {public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {...this.context = createWebApplicationContext(servletContext);...servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);...}}
 即把this.context(private WebApplicationContext context)放在ServletContext中,是能够在SpringMVC的controller方法中访问。


参考:

http://www.cnblogs.com/smarterplanet/p/4085620.html

https://openhome.cc/Gossip/SpringGossip/WebApplicationContext.html

http://hengyunabc.github.io/something-about-spring-mvc-webapplicationcontext/#

Demo:https://github.com/N3verL4nd/SpringSpringMVC.git

原创粉丝点击