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的机制,这里不再解释。
老的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 的学习,就是学习新的注解方式配置,网上很多。

原文地址:https://www.cnblogs.com/aji2014/p/6694361.html