WebApplicationInitializer in Spring MVC

来源:互联网 发布:linux编辑文件的命令 编辑:程序博客网 时间:2024/05/16 13:39

Spring 3.1 has introduced a new feature for configuring the deployment descriptor using the Java Config approach. We don’t need to write the web.xml file for initializing the web applications. Everything can be done through a Java class itself. The Java class has to implement the org.springframework.web.WebApplicationInitializer which will be loaded when web application starts. If you look at the example, a class implementing WebApplicationInitializer loads the spring context configuration file.

Lets look at the example.

1. Spring MVC Controller

SpringMVCController.java

package javabeat.net.spring.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class SpringMVCController {  @RequestMapping(value = "/springmvctest", method = RequestMethod.GET)  public String redirectTest(Model model) {    model.addAttribute("msg", "Test Message");    return "hello";  }}

2. WebApplicationInitializer

SpringWebAppInitializer.java

package javabeat.net.spring.controller;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.ServletRegistration;import org.springframework.web.WebApplicationInitializer;import org.springframework.web.context.support.XmlWebApplicationContext;import org.springframework.web.servlet.DispatcherServlet;public class SpringWebAppInitializer implements WebApplicationInitializer {    @Override    public void onStartup(ServletContext container) throws ServletException {  XmlWebApplicationContext appContext = new XmlWebApplicationContext();  appContext.setConfigLocation("/WEB-INF/spring-dispatcher-servlet.xml");  ServletRegistration.Dynamic dispatcher = container.addServlet(    "spring-dispatcher", new DispatcherServlet(appContext));  dispatcher.setLoadOnStartup(1);  dispatcher.addMapping("/");    }}

3. Views

hello.jsp

<html><body>  <h1>JavaBeat Spring MVC - WebApplicationInitializer</h1>  <h2>Value : ${msg}</h2></body></html>

4. Spring Configurations

spring-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"  xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:context="http://www.springframework.org/schema/context"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">  <context:component-scan base-package="javabeat.net.spring.controller" />  <bean id="jspViewResolver"    class="org.springframework.web.servlet.view.InternalResourceViewResolver">    <property name="prefix" value="/WEB-INF/jsp/" />    <property name="suffix" value=".jsp" />  </bean></beans>

5. Run WebApplicationInitializer using JavaConfig

You can just run the above spring mvc example without any extra changes. The class SpringWebAppInitializer will be loaded by the server and initialize the spring context. Note that, you have to use Spring 3.1 and Tomcat 7.0 to make it work. This feature is introduced from Servlet 3.0 release.

Download Spring MVC WebApplicationInitializer Example Source Code  

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 老板不安排工作怎么办 孩子啃老父母怎么办 孩子变成讨好形怎么办 工厂破产了工资怎么办 失业就业登记证怎么办? 如果父母是教师怎么办 老人档案丢了怎么办 想离婚想要孩子怎么办 一二年级成绩差怎么办 高考复读又失败怎么办 孩子不想估成绩怎么办 手机信号被屏蔽了怎么办 西安市旅游年卡怎么办 广电宽带网速慢怎么办 电视智能卡坏了怎么办 家乐卡二次授信怎么办? 邢台银行倒闭了怎么办 不敢和导师交流怎么办 考上一个破大学怎么办 腻子粉检测报告怎么办 电子厂插件很慢怎么办 预付费电表跳闸怎么办 电费有疑问怎么办大连 农村电表箱坏了怎么办 农村电表没电怎么办 家里电费特别高怎么办 电表读卡失败怎么办 电表卡消磁了怎么办 智能表采集失败怎么办 电脑没有蓝牙功能怎么办 判决后无力偿还怎么办 dz47-63c63跳闸怎么办 租房合同丢了怎么办 北京土地承包合同丢失怎么办 租赁合同丢失了怎么办 房屋租赁合同丢失怎么办 学校没发学生证怎么办 学校银行卡丢了怎么办 报税名字忘了怎么办 地税零申报漏报怎么办 欠中联重科施工电梯钱怎么办