SpringMVC基于代码的配置方式(零配置,无web.xml)
基于配置文件的web项目维护起来可能会更方便,可是有时候我们会有一些特殊的需求,比方防止客户胡乱更改配置,这时候我们须要给配置隐藏到代码中。
1.创建一个动态web项目(无需web.xml)
2.右键项目加入几个package: com.easyweb.config (保存项目配置) com.easyweb.controller (保存springMvc controller)
3.在 com.easyweb.config 新建一个类 WebApplicationStartup 。这个类实现WebApplicationInitializer 接口,是项目的入口,作用相似于web.xml,详细代码例如以下:
package com.easyweb.config;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
* server启动入口类
*
* @author Administrator
*
*/
public class WebApplicationStartup implements WebApplicationInitializer {
private static final String SERVLET_NAME = "Spring-mvc";
private static final long MAX_FILE_UPLOAD_SIZE = 1024 * 1024 * 5; // 5 Mb
private static final int FILE_SIZE_THRESHOLD = 1024 * 1024; // After 1Mb
private static final long MAX_REQUEST_SIZE = -1L; // No request size limit
/**
* server启动调用此方法,在这里能够做配置 作用与web.xml同样
*/
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// 注冊springMvc的servlet
this.addServlet(servletContext);
// 注冊过滤器
// servletContext.addFilter(arg0, arg1)
// 注冊监听器
// servletContext.addListener(arg0);
}
/**
* 注冊Spring servlet
*
* @param servletContext
*/
private void addServlet(ServletContext servletContext) {
// 构建一个application context
AnnotationConfigWebApplicationContext webContext = createWebContext(SpringMVC.class, ViewConfiguration.class);
// 注冊spring mvc 的 servlet
Dynamic dynamic = servletContext.addServlet(SERVLET_NAME, new DispatcherServlet(webContext));
// 加入springMVC 同意訪问的Controller后缀
dynamic.addMapping("*.html", "*.ajax", "*.css", "*.js", "*.gif", "*.jpg", "*.png");
// 所有通过请用 “/”
// dynamic.addMapping("/");
dynamic.setLoadOnStartup(1);
dynamic.setMultipartConfig(new MultipartConfigElement(null, MAX_FILE_UPLOAD_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD));
}
/**
* 通过自己定义的配置类来实例化一个Web Application Context
*
* @param annotatedClasses
* @return
*/
private AnnotationConfigWebApplicationContext createWebContext(Class<?
>... annotatedClasses) {
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.register(annotatedClasses);
return webContext;
}
}
4.在com.easyweb.config 下加入类 SpringMVC 继承 WebMvcConfigurerAdapter。这个类的作用是进行SpringMVC的一些配置,代码例如以下:
package com.easyweb.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
//指明controller所在的包名
@ComponentScan(basePackages = {"com.easyweb.controller"})
public class SpringMVC extends WebMvcConfigurerAdapter {
/**
* 非必须
*/
@Override
public void configureDefaultServletHandling(final DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* 假设项目的一些资源文件放在/WEB-INF/resources/以下
* 在浏览器訪问的地址就是相似:http://host:port/projectName/WEB-INF/resources/xxx.css
* 可是加了例如以下定义之后就能够这样訪问:
* http://host:port/projectName/resources/xxx.css
* 非必须
*/
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**/*").addResourceLocations("/WEB-INF/resources/");
}
}
5.加入view配置文件com.easyweb.config下新建类ViewConfiguration,里面能够依据自己的须要定义视图拦截器:
package com.easyweb.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles2.TilesConfigurer;
import org.springframework.web.servlet.view.tiles2.TilesView;
@Configuration
public class ViewConfiguration {
@Bean
public ViewResolver urlBasedViewResolver() {
UrlBasedViewResolver viewResolver;
viewResolver = new UrlBasedViewResolver();
viewResolver.setOrder(2);
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
// for debug envirment
viewResolver.setCache(false);
return viewResolver;
}
@Bean
public ViewResolver tilesViewResolver() {
UrlBasedViewResolver urlBasedViewResolver = new UrlBasedViewResolver();
urlBasedViewResolver.setOrder(1);
urlBasedViewResolver.setViewClass(TilesView.class);
//urlBasedViewResolver.
return urlBasedViewResolver;
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions(new String[] { "classpath:tiles.xml" });
return tilesConfigurer;
}
}
6.本例中还用了tiles视图解析器。替换了原始的include方式
7.完整代码已上传
http://download.csdn.net/detail/u013816347/8998891
学习路上,欢迎评论指正。
SpringMVC基于代码的配置方式(零配置,无web.xml)的更多相关文章
- WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用
本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...
- 如何用Java类配置Spring MVC(不通过web.xml和XML方式)
DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...
- SpringMVC基于代码的配置方式(零配置,无web.xml)直接继承WebMvcConfigurerAdapter
基于配置文件的web项目维护起来可能会更方便,但是有时候我们会有一些特殊的需求,比如防止客户胡乱更改配置,这时候我们需要给配置隐藏到代码中. 1.创建一个动态web项目(无需web.xml) 2.右键 ...
- springmvc学习指南 之---第27篇 spring如何实现servlet3.0无web.xml 配置servlet对象的
writedby 张艳涛 基于web.xml配置,有人说麻烦,tomcat给按照servlet3.0,实现了基于注解@WebServlet,有人说springmvc的springmvc.xml配置麻烦 ...
- 框架源码系列四:手写Spring-配置(为什么要提供配置的方法、选择什么样的配置方式、配置方式的工作过程是怎样的、分步骤一个一个的去分析和设计)
一.为什么要提供配置的方法 经过前面的手写Spring IOC.手写Spring DI.手写Spring AOP,我们知道要创建一个bean对象,需要用户先定义好bean,然后注册到bean工厂才能创 ...
- Centos6/7系统基础配置-从零到无
转至:https://www.cnblogs.com/Pigs-Will-Fly/p/13855300.html 目录 前言 系统配置 文档作用 一.Centos 6.X 系列配置 1.1 主机名 ...
- SpringMVC 学习 八 SSM环境搭建(一) web.xml配置
第一步:导入jar包 注意包的兼容性,以后采用maven会好很多 第二步:配置web.xml 在web.xml中,主要的配置内容有以下几点 (1)spring容器配置文件的位置 <!-- spr ...
- SpringMVC 零配置 无web.xml
对SpringMVC启动流程的讲解 https://www.cnblogs.com/beiyan/p/5942741.html 与SpringMVC的整合 https://hanqunfeng.ite ...
- 配置 struts2 时掉进 web.xml 的坑
这个声明不好用 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quo ...
随机推荐
- 设计模式之装饰器模式(PHP实现)
/** * 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. * 这种模式创建了一个 ...
- 让用VS2012/VS2013编写的程序在XP中顺利执行
微软为了推销自家平台,默认配置下VS2012和VS2013编写的应用程序仅仅能在Vista/Win7/Win8上执行.但幸好还保留了生成XP程序的设置项.XP和Win2003的用户还是大量存在的,我们 ...
- C源程序到可执行文件的四个过程
C源程序到可执行文件的四个过程 1.预处理:预编译器执行.譬如C中的宏定义就是由预编译器处理,注释等也是由预编译器处理的 gcc -E -hello.c -o hello.i 2.编译:编译器来执行. ...
- python 常用的模块(base64)转
Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...
- ubuntu+let's encrypt生成永久免费https证书 ubuntu+tomcat+nginx+let's encrypt
1. 下载let's encrypt $ sudo add-apt-repository ppa:certbot/certbot $ sudo apt-get update $ sudo apt-ge ...
- 最简单的基于FFmpeg的移动端样例附件:Android 自带播放器
===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...
- JavaScript Array splice函数
// 原来的数组 var array = ["one", "two", "four"]; // splice(position, numbe ...
- Calculation 2-欧拉函数的运用
Calculation 2 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit ...
- java模拟异步消息的发送与回调
http://kt8668.iteye.com/blog/205739 本文的目的并不是介绍使用的什么技术,而是重点阐述其实现原理. 一. 异步和同步 讲通俗点,异步就是不需要等当前执行的动作完成 ...
- 解决长时间计划任务rsync同步进程数过多
用rsync同步远程服务器,由于设置的的同步间隔较短(5分钟),这样一旦网速问题导致5分钟内同步不完.就会倒是同步紊乱,导致系统中很多rsync进程(# ps -aux | grep rsync) ...