spring注解与springMVC注解扫描的问题

  在将spring与springMVC结合使用时,当咱们使用注解的时候,通常都是在spring配置文件中配置注解扫描dao层、service层的包,在springMVC配置文件中配置注解扫描controller,本身在练习spring+SpringMVC+mybatis的项目时对这种作法只知其一;不知其二,因此在练习项目的时候在实践中对本身的一些想法进行了验证。web

  • 通常的配置
    •   spring配置文件中
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:aop="http://www.springframework.org/schema/aop"
              xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:context="http://www.springframework.org/schema/context"   
              xsi:schemaLocation="
                  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
                  
           <!-- 启用注解  -->
          <context:annotation-config />
      
          <!-- 设置使用注解的类所在的包 -->
          <context:component-scan base-package="dao,service.*"></context:component-scan>
          
          <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                  <list>
                      <value>classpath:config/jdbc.properties</value>
                  </list>
              </property>
          </bean>
          <!-- c3p0链接池配置 -->  
           <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
                 <!-- 四大参数的name不能换,和链接池的构造函数有关-->
                <property name="driverClass" value="${jdbc.driverClassName}"/>
                <property name="jdbcUrl" value="${jdbc.url}" />
                <property name="user" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />  
        
                 <!--链接池中保留的最大链接数。默认值: 15 -->   
                <property name="maxPoolSize" value="20"/>  
                <!-- 链接池中保留的最小链接数,默认为:3-->  
                <property name="minPoolSize" value="2"/>  
                <!-- 初始化链接池中的链接数,取值应在minPoolSize与maxPoolSize之间,默认为3-->  
                <property name="initialPoolSize" value="2"/>  
        
                <!--最大空闲时间,60秒内未使用则链接被丢弃。若为0则永不丢弃。默认值: 0 -->   
                <property name="maxIdleTime" value="60"/>  
                  
                <!-- 当链接池链接耗尽时,客户端调用getConnection()后等待获取新链接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒。默认: 0 -->   
                <property name="checkoutTimeout" value="3000"/>  
                  
                <!--当链接池中的链接耗尽的时候c3p0一次同时获取的链接数。默认值: 3 -->   
                <property name="acquireIncrement" value="2"/>  
        
               <!--定义在从数据库获取新链接失败后重复尝试的次数。默认值: 30 ;小于等于0表示无限次-->   
                <property name="acquireRetryAttempts" value="0"/>  
        
                <!--从新尝试的时间间隔,默认为:1000毫秒-->   
                <property name="acquireRetryDelay" value="1000" />  
        
                <!--关闭链接时,是否提交未提交的事务,默认为false,即关闭链接,回滚未提交的事务 -->   
                <property name="autoCommitOnClose" value="false"/>  
         
                <!--c3p0全局的PreparedStatements缓存的大小。若是maxStatements与maxStatementsPerConnection均为0,则缓存不生效,只要有一个不为0,则语句的缓存就能生效。若是默认值: 0-->   
                <property name="maxStatements" value="100"/>  
                <!--maxStatementsPerConnection定义了链接池内单个链接所拥有的最大缓存statements数。默认值: 0 -->   
                <property name="maxStatementsPerConnection" value="0"/>  
           </bean>
           <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
            <!-- 配置mybatis配置文件 -->
            <property name="configLocation">
               <value>classpath:config/sqlMapConfig.xml</value>
            </property>
             <!-- 实体类映射文件路径,能够在这里直接配置,也能够在configLocation配置的文件中配置 ibatis2不支持 -->  
             <!-- <property name="mapperLocations" value="classpath:config/sqlmap/*.xml" />  -->
          </bean>
          <!-- Dao -->
          <bean id="ibatisDao" class="dao.IbatisDao">
               <property name="dataSource" ref="dataSource"></property> 
               <!-- 这里的dao继承SqlMapClientDaoSupport,因此要显示的配置注入 sqlMapClient-->
                <property name="sqlMapClient">
                  <ref bean="sqlMapClient"/>  
               </property>   
          </bean>
          <!-- 开启事务注解 -->
          <tx:annotation-driven transaction-manager="transactionManager"/>
          <!-- 定义事务管理器(声明式的事务) -->  
          <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
              <property name="dataSource" ref="dataSource" />
          </bean>
          
      </beans>

      这里咱们配置了事务管理器和基本的dao。service层采用的是注解注入ajax

    •  springMVC配置文件中:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"     
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"     
              xmlns:context="http://www.springframework.org/schema/context"     
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
      <!--     启用spring mvc 注解 -->
          <context:annotation-config />
      
          <!-- 设置使用注解的类所在的包 -->
         <context:component-scan base-package="controller"></context:component-scan>   
           
          <!-- 完成请求和注解POJO的映射  3.1后已通过时 与DefaultAnnotationHandlerMapping配套使用
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />-->
          <!-- 新的注解映射-->
          <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
          
          
           <!-- 使用新的注解映射必须指定 RequestMappingHandlerMapping去替代过期的DefaultAnnotationHandlerMapping-->
           <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
           <!-- 使用ajax向前台返回json须要在注解映射中添加转换器 -->
           <!-- 依赖包为jackson-core 、 jackson-annotation、 jackson-databind -->
           <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
                    <property name="messageConverters">
                        <list>
                            <ref bean="jsonHttpMessageConverter"/>
                        </list>
                    </property>
          </bean>
      <bean id="jsonHttpMessageConverter" 
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /><!--   用于jackson2 -->
           <!--  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> -->
          
          <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/page/" p:suffix=".jsp" />
      </beans>

      springMVC配置文件中配置了指定的HandlerAdapter和HandlerMapping以及Resolver。
      在这种配置下,项目能正常运行,配置了@Transactional的service中的事务也能被咱们的事务管理器管理。spring

  • 只在spring配置文件中启用注解扫描(关闭springmvc扫描),扫描dao、service、controller。
      这种状况下,项目启动正常,可是没法处理咱们的请求,鉴于springmvc容器是spring容器的子容器,我猜测因为controller中的全部请求处理方法我都是采用@requestMapping来进行配置的,requestMapping都是由咱们的springMVC配置文件中配置的HandlerAdapter和HandlerMapping以及Resolver的Bean来进行处理(即便咱们不在这里面配置,spingmvc也有默认的),可是父容器又不能访问子容器的bean,因此即便扫描了controller包也没法将controller的处理方法与咱们的请求正确mapping,因此这里若是仅仅在spring中扫描全部包,项目是没法实现正常运转的。
  • 只在spring配置文件中启用注解扫描,扫描dao、service、controller,而且在spring配置文件中配置springmvc的HandlerAdapter和HandlerMapping以及Resolver。
      这种状况项目与咱们通常配置的效果同样,这种状况就至关于咱们在spring中也完成了spingmvc的配置,将本来应该在springmvc容器中的bean直接在spring中配置,虽然效果是同样的,可是不建议这种作。这种配置的结果也表明咱们上一种状况出现的缘由的猜测是对的,spring父容器没法访问springmvc中的bean,没法将扫描到的controller与请求url正确适配。
  • 在springMVC配置文件中启用注解扫描(关闭spring扫描),扫描dao、service、controller。
      这种状况下项目能正常跳转,可是咱们经过@Transactional配置的service事务失效,这里是由于spring容器配置了事务管理器后,装配的service都是通过cglib代理过的实例,具备事务功能,而springmvc容器装配的service是没有进行代理处理的,因此是没有事务功能的,这一点能够参见这篇博客http://blog.csdn.net/haoyifen/article/details/51172647。这里我试着在springMVC配置文件中也配置了一个事务管理器事后,发现一切正常了,由springmvc装配的service一样也具有事务功能,这里就很奇怪,为何父容器配置的事务管理器子容器没法使用呢?父容器中的bean子容器都是能够访问的,为何没有使用呢?到这里差很少能够回答开始的问题了,为何要在spring中扫描dao、service,由于经过spring容器扫描dao以及service能够进行事务的加强,若是你仅仅在一个子容器中进行事务的加强那么其余的serviceBean是不会被事务加强的(好比第四种状况中,若是咱们在spring容器中手动配置一个bean,那么这个bean不是由springmvc装配的,而咱们的事务管理器在springmvc中,这个bean就不具有事务的功能)。而controller之因此要在springmvc中配置,由于spring容器没法访问到springmvc容器的bean(HandlerAdapter和HandlerMapping以及Resolver等),没法完成请求与hander的正确适配,若是要强行实现,在spring中配置HandlerAdapter和HandlerMapping以及Resolver则显得太过牵强。在书上看到的一种说法是这样也体现了分层的概念,dao、service等web层都须要使用的bean应该直接在spring容器中装配,而controller这种则放在专门处理请求跳转的springmvc容器中,相对于将全部bean配置在spring容器中,也体现了权限控制的思想吧。
相关文章
相关标签/搜索