Web上下文配置【MvcConfig】

基于Servlet3.0规范和SpringMVC4注解式配置方式,实现零xml配置,弄了个小demo,供交流讨论。

项目说明如下

1.db.sql是项目中用到的表,数据库使用的是oracle11g

2.该项目使用mvn进行管理,私服为自搭建nexus,项目只用到一个第三方 jar,就是oracle的驱动;

3.默认项目为零配置启动,如果需要更改启动方式,请作如下操作:

<1.使用xml风格测试时,需要先将/src/main/java/web/config下的全部类移动到项目外,并开启web.xml中的相关配置>

<2.使用Bean风格测试时,需要保证/src/main/java/web/config下的全部类都存在,并关闭web.xml中的相关配置>

4.项目特征如下:

1)项目基于SpringMVC4

2)安全控制使用SpringSecurity3.2

3)Hibernate4 + 注解式事务管理

4)基于ASpect的注解式AOP

5)基于EHCache的注解式缓存

6)JSON及XML例子,@RestController及@Controller使用对比

7)单元测试基于MockMvc

8)配置了较完整的组件,贴近实际应用

5.运行环境:tomcat8.0.9 jdk1.7

这里对SpringMVC零配置做一个简单的说明

spring4中提供了大量的注解来支持零配置,简要说明如下:

@Configuration : 类似于spring配置文件,负责注册bean,对应的提供了@Bean注解。需要org.springframework.web.context.support.AnnotationConfigWebApplicationContext注册到容器中。

@ComponentScan : 注解类查找规则定义 <context:component-scan/>

@EnableAspectJAutoProxy : 激活Aspect自动代理 <aop:aspectj-autoproxy/>

@Import @ImportResource: 关联其它spring配置  <import resource="" />

@EnableCaching :启用缓存注解  <cache:annotation-driven/>

@EnableTransactionManagement : 启用注解式事务管理 <tx:annotation-driven />

@EnableWebMvcSecurity : 启用springSecurity安全验证

Servlet3.0规范,支持将web.xml相关配置也硬编码到代码中[servlet,filter,listener,等等],并由javax.servlet.ServletContainerInitializer的实现类负责在容器启动时进行加载,

spring提供了一个实现类org.springframework.web.SpringServletContainerInitializer,

该类会调用所有org.springframework.web.WebApplicationInitializer的实现类的onStartup(ServletContext servletContext)方法,将相关的组件注册到服务器;

spring同时提供了一些WebApplicationInitializer的实现类供我们继承,以简化相关的配置,比如:

org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer : 注册spring DispatcherServlet

org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer : 注册springSecurity

同时,spring也提供了一些@Configuration的支持类供我们继承,以简化相关@Configuration的配置,比如:

org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport : 封装了springmvc相关组件,我们可以通过注册新的@Bean和@Override相关方法,以实现对各个组件的注册;

org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter : 封装类springsecurity相关组件

源文件可找我要!  web-mvc.zip

时间: 2024-08-30 12:23:15

Web上下文配置【MvcConfig】的相关文章

SpringMVC4零配置--Web上下文配置【MvcConfig】

与SpringSecurity的配置类似,spring同样为我们提供了一个实现类WebMvcConfigurationSupport和一个注解@EnableWebMvc以帮助我们减少bean的声明. applicationContext-MvcConfig.xml Xml代码   <!-- 启用注解,并定义组件查找规则 ,mvc层只负责扫描@Controller --> <context:component-scan base-package="web.function&quo

Spring MVC的web.xml配置详解(转)

出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.

spring中的web上下文,spring上下文,springmvc上下文区别(超详细)

web上下文(Servlet context),spring上下文(WebApplication Context),springmvc上下文(mlWebApplicationCont)之间区别. 上下文:可以简单的理解为容器,配置文件 web上下文目标对象是所有web应用,spring上下文目标对象是单个web应用,spring mvc目标对象是单个web应用的spring mvc框架(是spring上下文的子上下文,即继承自spring上下文,所以子能够调用父的东西,反之,不可). 以上感觉自

java web.xml配置详解

1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文. 3.容器将<context-param>转换为键值对,并交给servletContext. 4.容器创建<listener>中的类实例,创建监听器. 二  Load-on-startup Load

【Web.xml配置具体解释之context-param 】

转自:http://blog.csdn.net/liaoxiaohua1981/article/details/6759206 格式定义: [html] view plaincopy <context-param> <param-name>contextConfigLocation</param-name> <param-value>contextConfigLocationValue></param-value> </context

web.xml配置之&lt;context-param&gt;详解

读完本文,你将了解(1)<context-param>的作用和用法,(2)<context-param>配置和<init-param>的区别 <context-param>的作用和用法: 1.<context-param>配置是是一组键值对,比如: <context-param>        <param-name>home-page</param-name>        <param-value&g

160329(二)、web.xml配置详解

1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文. 3.容器将<context-param>转换为键值对,并交给servletContext. 4.容器创建<listener>中的类实例,创建监听器. 二  Load-on-startup Load

web.xml 配置介绍

这个不是原创,有点早了,具体从哪里来的已经记不得了.但是东西是实实在在的. 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文. 3.容器将<context-param>转换为键值对,并交给servletContext. 4.容器创建<listener&g

springmvc web.xml配置之 -- DispatcherServlet

springMVC servlet配置与启动 看一下springmvc的web.xml常见配置: <servlet> <!-- 配置DispatcherServlet --> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定s