springMVC Interceptor遇到的问题

项目中spring版本使用的是
4.0.0.RELEASE
IOC容器配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 启用spring mvc 注解 -->
    <context:annotation-config/>
    <!-- 自动扫描dao和service包(自动注入) -->
    <context:component-scan base-package="com.*.*"/>
    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:config.properties" ignore-resource-not-found="true"
                                  ignore-unresolvable="true"/>
    <!-- 配置数据源 -->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialPoolSize" value="${connection_pools.initial_pool_size}"/>
        <property name="minPoolSize" value="${connection_pools.min_pool_size}"/>
        <property name="maxPoolSize" value="${connection_pools.max_pool_size}"/>
        <property name="maxIdleTime" value="${connection_pools.max_idle_time}"/>
        <property name="acquireIncrement" value="${connection_pools.acquire_increment}"/>
        <property name="checkoutTimeout" value="${connection_pools.checkout_timeout}"/>
    </bean>
    <!-- 配置hibernate session工厂 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 依赖注入数据源,正是上文定义的dataSource -->
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate4.SpringSessionContext
                </prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.net.sf.ehcache.configurationResourceName">classpath:ehcache.xml</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
        </property>
        <!-- 自动扫描注解方式配置的hibernate类文件    -->
        <property name="packagesToScan" value="com.xxdc.weizhu.entity"/>
        <!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
        <!--
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:sy/hbm</value>
            </list>
        </property>
         -->
    </bean>
    <!-- 日志 -->
    <aop:aspectj-autoproxy/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!--  -->
    <!-- ehchace缓存管理器:ehcache缓存大对象 -->
    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <!-- 注入缓存配置文件的位置 -->
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    <!-- 配置Spring的缓存管理器 -->
    <!--    <bean id="springCacheManagerSpring" class="org.springframework.cache.ehcache.EhCacheCacheManager">
                  注入ehcache的管理器 -
                  <property name="cacheManager" ref="ehCacheManager"/>
       </bean> -->

    <!-- 配置缓存的注解驱动,它会自动到spring的bean中寻找缓存相关注解,并使其有效 -->
    <!--  <cache:annotation-driven cache-manager="springCacheManagerSpring"/> -->
    <!-- 配置事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 完成请求和注解POJO的映射 -->
    <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!-- json string编码 -->
                <ref bean="stringHttpMessageConverter"/>
                <!-- json转换器  -->
                <ref bean="mappingJacksonHttpMessageConverter"/>
            </list>
        </property>
    </bean>
    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <!-- <property name="supportedMediaTypes">
            <list>
                <value>application/html;charset=UTF-8</value>
            </list>
        </property> -->
    </bean>
    <!-- 解决返回json字符串编码 -->
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/html;charset=UTF-8</value>
                <value>text/plain;charset=UTF-8</value>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 定义跳转的文件的前后缀 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
    </bean>
    <!-- 文件上传 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    <!-- <mvc:resources location="/resources/"  mapping="/resources/**" /> -->
    <!-- <mvc:resources location="/h5/"  mapping="/h5/**" /> -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/><!--/**表示任意层级的任意路径-->
            <bean class="com.xxdc.weizhu.log.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
    <!--<mvc:annotation-driven/>-->
</beans>

最后一行 <mvc:annotation-driven/>如果被注释掉了在拦截器里的代码如下

Class<?> aClass = handler.getClass();
HandlerMethod hm = (HandlerMethod) handler;

aClass的类型是(具体类名被隐去)

class com.*.*.HouseDetailsAction$$EnhancerByCGLIB$$54c148a7

显然是被CGLI代理过的,这样在强转成HandlerMethod的时候被抛出异常

Caused by: java.lang.ClassCastException: EnhancerByCGLIB54c148a7 cannot be cast to org.springframework.web.method.HandlerMethod

如果使<mvc:annotation-driven/>生效启动时会报这个异常

Caused by: javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

于是添加了这个依赖就好

<dependency>
  <groupId>org.hibernate.validator</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>6.0.2.Final</version>
</dependency>

总结
目前不知道具体是什么原因导致的,但是问题是解决了:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值