spring启动,spring mvc ,要不要xml配置,基于注解配置
老项目是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的机制,这里不再解释。<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
http://static.springsource.org/spring/docs/3.0.x/changelog.txt
里面注明:
removed ContextLoaderServlet and Log4jConfigServlet
<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配置,基于注解配置的更多相关文章
- WebApplicationInitializer究 Spring 3.1之无web.xml式 基于代码配置的servlet3.0应用
本文转自http://hitmit1314.iteye.com/blog/1315816 大家应该都已经知道Spring 3.1对无web.xml式基于代码配置的servlet3.0应用.通过spri ...
- Spring学习记录(十二)---AOP理解和基于注解配置
Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- Spring MVC 5 + Thymeleaf 基于Java配置和注解配置
Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...
- Spring Boot中的缓存支持(一)注解配置与EhCache使用
Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...
- java spring是元编程框架---使用的机制是注解+配置
java spring是元编程框架---使用的机制是注解+配置
- Spring介绍及配置(XML文件配置和注解配置)
本节内容: Spring介绍 Spring搭建 Spring概念 Spring配置讲解 使用注解配置Spring 一.Spring介绍 1. 什么是Spring Spring是一个开源框架,Sprin ...
- spring mvc 基于注解 配置默认 handlermapping
spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
随机推荐
- Java算法练习——回文数
题目链接 题目描述 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1 输入: 121 输出: true 示例 2 输入: -121 输出: false ...
- Day1-T2
原题目 在小X的认知里,质数是除了本身和1以外,没有其他因数的数. 但由于小 X对质数的热爱超乎寻常,所以小X同样喜欢那些虽然不是质数, 但却是由两个质数相乘得来的数. 于是,我们定义一个数小 X喜欢 ...
- Python属性和内建属性
属性property 1. 私有属性添加getter和setter方法 class Money(object): def __init__(self): self.__money = 0 def ge ...
- OC项目加入swift第三方库遇到的坑
https://www.jianshu.com/p/96d868dcd69c 2017.07.07 16:23* 字数 295 阅读 5218评论 2喜欢 4 首先,在OC项目的Podfile文件中添 ...
- PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- 简单makefile示例
Makefile cmd: - g++ 相信在linux下编程的没有不知道makefile的,刚开始学习linux平台下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其 ...
- 浅谈无参数RCE
0x00 前言 这几天做了几道无参数RCE的题目,这里来总结一下,以后忘了也方便再捡起来. 首先先来解释一下什么是无参数RCE: 形式: if(';' === preg_replace('/[^\W] ...
- 题解 P1220 【关路灯】
区间DP, 考虑设\(dp[i][j][t]\)为已经关掉了\([i,j]\)的电灯, 人在t端点处时的最小代价 可以推出方程: \[ dp[i+1][j][0]+(p[n]-p[j]+p[i])*( ...
- Lock wait timeout exceeded; try restarting transaction(mysql事务锁)
现场环境客户要求删数据(界面没法直接操作),于是直接在数据库进行查询删除了,删完发现界面依然能查到删除后的数据,又用sql语句进行删除,发现报了错:Lock wait timeout exceeded ...
- Swift 中调试状态下打印日志
首先我们应该知道Swift中真个程序的入口就是在AppDelegate.swift中.所以在打印日志在 AppDelegate.swift中是这样的 import UIKit @UIApplicati ...