老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置。web.xml中也配置了一大堆。

现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用了少量的必要的spring xml配置component-scan之类,其实是结合使用,最近看了spring的书,说可以完全去掉xml,用@Configuration @EnableWebMvc和 基于javaConfig配置形式。 现在又知道有servlet 3 以后,可以连web.xml 都不要了。搞的晕头转向。上网搜了一下,乱记录一下。

============

spring 可以单独使用。但是一般项目用到的都是spring 在java web 应用中的应用。

其实spring 管理bean的功能 和 aop的功能单独使用(不用于web项目)也很正常。

springmvc是 可以替代以前spring+struts的形式中的struts,其实处理的是请求转发、参数映射等问题。现在一般用spring+springMVC。

以前web项目spring配置 大都是 基于xml配置方式,如web.xml、applicationContext.xml,现在大都是基于 注解方式 +xml配置,或者 注解+javaConfig 方式(几乎去掉了xml),Servlet 3 再去掉web.xml,几乎就没有xml了。

========================

spring 和 springmvc的区别,不再解释。不是一个东西。spring 有spring的配置,spring mvc有springmvc的配置形式。当然springmvc是基于spring的。

一般java web项目中spring的配置是 配置bean(applicationContext-*.xml) , web.xml中配置contextConfigLocation 和Listener:

1.web.xml 中配置spring 配置信息

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param>

2.web.xml 中配置一个监听器:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener加载bean的机制,这里不再解释。
老的spring版本中 还可以用servlet方式启动。
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
还有Log4jConfigServlet  方式。不过 这些都不在推荐了。这种方式,spring3.0以后不再支持,建议使用监听器方式。你可以查看一下spring3.0的change log 
http://static.springsource.org/spring/docs/3.0.x/changelog.txt 
里面注明: 
removed ContextLoaderServlet and Log4jConfigServlet
还有通过plugin配置方式:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" />
</plug-in>

该方式适用于,spring与struts等整合,在Struts的配置文件struts-config.xml里面配置一个ContextLoaderPlugIn,用于spring的初始化工作。

参考http://www.cnblogs.com/duanxz/p/5074584.html 在Web项目中,启动Spring容器的方式有三种,ContextLoaderListener、ContextLoadServlet、ContextLoaderPlugin

============

springmvc的配置是:配置 springmvc.xml,web.xml中配置 DispatcherServlet

<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- ContextconfigLocation配置springmvc加载的配置文件
适配器、处理映射器等
-->
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/spring/springmvc-servlet.xml.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 1、.action访问以.action结尾的 由DispatcherServlet进行解析
2、/,所有访问都由DispatcherServlet进行解析
-->
<url-pattern>/</url-pattern>
</servlet-mapping>

springmvc-servlet.xml一般配置是:

配置 <mvc:annotation-driven />

配置controller扫描等<context:component-scan base-package="com.xxx.controller">

配置视图解析器,拦截器,异常处理器等。

==========

关于spring +springmvc中两个spring应用上下文(DispatcherServlet和ContextLoaderListener)的问题,挺让人迷糊的。

他们都是加载Bean。简单粗暴的理解就是spring的bean 用ContextLoaderListener加载,springmvc的用DispatcherServlet 加载。

《spring in action》一书中给了点解释,【我们希望DispatcherServlet 加载包含Web组件的bean,如控制器,视图解析器及处理映射,而ContextLoaderListener需要加载应用中的其他bean。这些bean通常是驱动应用后端的中间层和数据层组件】。

那按道理说,反正都是bean的配置,所有的配置都 配置到一起也是可以的? 其实不然。

参看另一篇随笔 DispatcherServlet和ContextLoaderListener,还有spring+servlet3.0 无web.xml启动问题 http://www.cnblogs.com/aji2014/p/6702365.html

个人觉得,还是分开。否则可能引起不必要的错误。

==========================================

spring单独用,启动加载的问题。

web下的spring启动,就不说了。靠的是web的servlet 或者listener。 如果没有servlet或者仅仅是单元测试,例如在main函数里启动,有几种方式:

1.基于xml配置文件的

例如 ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");

或者常用的 ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml")

2. 基于注解的方式

JavaConfigApplicationContext context = new JavaConfigApplicationContext(AppConfig.class, DataConfig.class);

 AccountService accountService = (AccountService) context.getBean("accountService");
 @Configuration
public abstract class AppConfig {
@Bean
public AccountService accountService() {
return new AccountService(dataSource());
}
@ExternalBean
public abstract DataSource dataSource();
}
@Configuration
public abstract class DataConfig {
@Bean
public DataSource dataSource() {
return new DataSource(...);
}
}

参见spring文档http://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/apidocs/org/springframework/config/java/context/JavaConfigApplicationContext.html

3. 基于注解 ,用于单元测试的方式

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {
@Autowired
private CompactDisc cd;
@Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
}
}
@ContextConfiguration(classes=CDPlayerConfig.class) 用于javaConfig配置形式。
对于xml形式,可用@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" }) 形式

=============

关于去掉所有的xml配置,包括web.xml ,springmvc.xml,application-context.xml 问题:

参见 【Servlet 3 + Spring MVC零配置:去除所有xml   http://blog.csdn.net/xiao__gui/article/details/46803193 】

servlet3.0 的学习,就是学习新的注解方式配置,网上很多。

spring启动,spring mvc ,要不要xml配置,基于注解配置的更多相关文章

  1. WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用

    本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...

  2. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  3. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  4. Spring MVC 5 + Thymeleaf 基于Java配置和注解配置

    Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...

  5. Spring Boot中的缓存支持(一)注解配置与EhCache使用

    Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...

  6. java spring是元编程框架---使用的机制是注解+配置

    java spring是元编程框架---使用的机制是注解+配置

  7. Spring介绍及配置(XML文件配置和注解配置)

    本节内容: Spring介绍 Spring搭建 Spring概念 Spring配置讲解 使用注解配置Spring 一.Spring介绍 1. 什么是Spring Spring是一个开源框架,Sprin ...

  8. spring mvc 基于注解 配置默认 handlermapping

    spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...

  9. Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)

    参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...

随机推荐

  1. NSPredicate实现数据筛选

    一:基本语法 1.什么是NSPredicate apple官方文档这样写的: The NSPredicate class is used to define logical conditions us ...

  2. POJ2002 &&HDU5365 判断给定的点中有多少个不同的正方形

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17740   Accepted: 6776 Descript ...

  3. POJ 3663:Costume Party

    Costume Party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12607   Accepted: 4977 De ...

  4. 4 react 简书 引入 redux 的 combineReducers 对 redux 数据进行管理

    1. src 下的 common 下的 header 创建 store 文件夹 下创建 reducer.js # src/common/header/store/reducer.js const st ...

  5. 16 react 发送异步请求获取数据 和 使用Redux-thunk中间件进行 ajax 请求发送

    1.发送异步请求获取数据 1.引入 axios ( 使用 yarn add axios 进行安装 ) import axios from 'axios'; 2. 模拟 在元素完成挂载后加载数据 并初始 ...

  6. python 中的os.path.split()函数用法

    基本概念   os.path.split()通过一对链表的头和尾来划分路径名.链表的tail是是最后的路径名元素.head则是它前面的元素. 举个例子: path name = '/home/User ...

  7. SQL中的LEFT/RIGHT/SUBSTRING函数

    语法: LEFT(field,length) RIGHT(field,length)SUBSTRING(field,start,length) LEFT/RIGHT函数返回field最左边/最右边的l ...

  8. 干货分享|Law Essay写作高分攻略

    很多法学院的留学生对于Law Essay写作不是特别擅长,理论知识都了解,但是写出来的essay分数就是不高.同学们要从哪些方面入手呢?Law Essay写作要怎么拿高分?具体就跟小编一起来看看吧! ...

  9. docker搭建redis主从集群和sentinel哨兵集群,springboot客户端连接

    花了两天搭建redis主从集群和sentinel哨兵集群,讲一下springboot客户端连接测试情况 redis主从集群 从网上查看说是有两种方式:一种是指定配置文件,一种是不指定配置文件 引用地址 ...

  10. 常用函数式接口与Stream API简单讲解

    常用函数式接口与Stream API简单讲解 Stream简直不要太好使啊!!! 常用函数式接口 Supplier<T>,主要方法:T get(),这是一个生产者,可以提供一个T对象. C ...