spring cglib报错 xxx cannot be cast to xxx

java爱好者 2015-01-18 01:21:14
1, 想使用 ehcache + spring jdbc 实现查询结果缓存,现在功能都实现了,但是使用时报错


java.lang.ClassCastException: com.hbzx.bean.ShopViewCountBean cannot be cast to java.util.List
at com.hbzx.dao.ShopViewCountDao$$EnhancerBySpringCGLIB$$1c70ff76.queryAll(<generated>)
at com.hbzx.service.ShopViewCountService.fillHistory(ShopViewCountService.java:143)
at com.hbzx.service.ShopViewCountService.query(ShopViewCountService.java:46)
at com.hbzx.controller.UserCommissionController.getMyShopLog(UserCommissionController.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:743)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:672)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:82)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:919)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:180)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)


...全文
2373 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
猿人林克 2015-01-21
  • 打赏
  • 举报
回复
引用 2 楼 afgasdg 的回复:
报错类:

package com.hbzx.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.hbzx.bean.ShopViewCountBean;


@Repository
public class ShopViewCountDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;


       /**
       *  其他方式使用注解没有报错,只有这个方法使用注解缓存结果就报错,可能和泛型有关系
       */
	@Cacheable(value = "queryCache", key = "#shopId")
	public List<ShopViewCountBean> queryAll(Long shopId) {
		if (shopId == null)
			return null;
		String sql = "select * from  shop_view_count where shop_id = ?";
		return jdbcTemplate.query(sql, ShopViewCountBean.getRowMap(), shopId);
	}

	
	@CacheEvict(value = "queryCache", key = "#shopId")
	public int update(Long shopId, Long startTime, Long endTime) {
		String sql = "insert into shop_view_count SELECT shop_id, type, if(type = 2,(count(DISTINCT uuid) + IFNULL((select sum(pviewcount) from shop_pviewcount_prize as spp  where spp.shop_id = vl.shop_id and spp.updated_time > FROM_UNIXTIME(?,'%Y-%m-%d') and spp.updated_time <= FROM_UNIXTIME(?,'%Y-%m-%d')  ), 0)),count(DISTINCT uuid) ) as number, ? as update_time FROM 	view_log as vl where vl.created_time > ?  and vl.created_time <= ? and vl.shop_id = ?   GROUP BY 	vl.shop_id, 	vl.type  on duplicate key update number = VALUES(number)  + number";
		jdbcTemplate.update(sql, startTime, endTime, endTime, startTime, endTime, shopId);
		sql = "update shop_view_count set update_time =? where shop_id = ?";
		return jdbcTemplate.update(sql, endTime, shopId);
	}


	@Cacheable(value = "queryCache", key = "#shopId")
	public ShopViewCountBean queryOne(Long shopId) {
		try {
			String sql = "select *  from shop_view_count where shop_id = ? limit 1 ";
			return jdbcTemplate.queryForObject(sql, ShopViewCountBean.getRowMap(), shopId);
		} catch (Exception e) {
			return null;
		}
	}
}

/** * 其他方式使用注解没有报错,只有这个方法使用注解缓存结果就报错,可能和泛型有关系 */ select * from shop_view_count where shop_id = ? 你这个sql实际去库里查到底返回了几条数据?目测你这错误,该结果只有一条。 和你的缓存没有关系
  • 打赏
  • 举报
回复
queryAll 查询出来的结果是ShopViewCountBean ,但是你返回的是个list。所以不能转换
淡定的峰哥 2015-01-19
  • 打赏
  • 举报
回复
queryAll与queryOne使用的缓存一样(value一样),key的类型一样,这样肯定会冲突,将两个方法的key写成不一样
java爱好者 2015-01-18
  • 打赏
  • 举报
回复
使用方式:

@Service
public class ShopViewCountService {
	@Autowired
	private ShopViewCountDao shopViewCountDao;
	@Autowired
	private ViewLogDao viewLogDao;
	@Autowired
	private SysConfigDao sysConfigDao;

	/**
	 * 查询店铺基本访问信息
	 * 
	 * @author LiuJunGuang
	 * @param shopId 店铺号
	 * @return
	 * @date 2015年1月16日下午4:30:14
	 */
	public ShopPerformanceBean query(Long shopId) {
		updateHistory(shopId);// 更新历史记录统计信息
		// 填充今天开始时间历史记录信息
		ShopPerformanceBean bean = new ShopPerformanceBean(shopId);
		fillHistory(bean);// 填充历史访问量
		return bean;
	}

     private void fillHistory(ShopPerformanceBean bean) {
		List<ShopViewCountBean> result = shopViewCountDao.queryAll(bean.getShopId());
               //省略N行代码
    }
java爱好者 2015-01-18
  • 打赏
  • 举报
回复
报错类:

package com.hbzx.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.hbzx.bean.ShopViewCountBean;


@Repository
public class ShopViewCountDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;


       /**
       *  其他方式使用注解没有报错,只有这个方法使用注解缓存结果就报错,可能和泛型有关系
       */
	@Cacheable(value = "queryCache", key = "#shopId")
	public List<ShopViewCountBean> queryAll(Long shopId) {
		if (shopId == null)
			return null;
		String sql = "select * from  shop_view_count where shop_id = ?";
		return jdbcTemplate.query(sql, ShopViewCountBean.getRowMap(), shopId);
	}

	
	@CacheEvict(value = "queryCache", key = "#shopId")
	public int update(Long shopId, Long startTime, Long endTime) {
		String sql = "insert into shop_view_count SELECT shop_id, type, if(type = 2,(count(DISTINCT uuid) + IFNULL((select sum(pviewcount) from shop_pviewcount_prize as spp  where spp.shop_id = vl.shop_id and spp.updated_time > FROM_UNIXTIME(?,'%Y-%m-%d') and spp.updated_time <= FROM_UNIXTIME(?,'%Y-%m-%d')  ), 0)),count(DISTINCT uuid) ) as number, ? as update_time FROM 	view_log as vl where vl.created_time > ?  and vl.created_time <= ? and vl.shop_id = ?   GROUP BY 	vl.shop_id, 	vl.type  on duplicate key update number = VALUES(number)  + number";
		jdbcTemplate.update(sql, startTime, endTime, endTime, startTime, endTime, shopId);
		sql = "update shop_view_count set update_time =? where shop_id = ?";
		return jdbcTemplate.update(sql, endTime, shopId);
	}


	@Cacheable(value = "queryCache", key = "#shopId")
	public ShopViewCountBean queryOne(Long shopId) {
		try {
			String sql = "select *  from shop_view_count where shop_id = ? limit 1 ";
			return jdbcTemplate.queryForObject(sql, ShopViewCountBean.getRowMap(), shopId);
		} catch (Exception e) {
			return null;
		}
	}
}

java爱好者 2015-01-18
  • 打赏
  • 举报
回复
配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    default-autowire="byName"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd   
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd "> 
   
    <!-- 配置Ehcache缓存管理器,读取配置文件 -->
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:/conf/spring/ehcache.xml"></property>
    </bean>
    <!-- 配置缓存管理器,获取cache -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >
          <property name="cacheManager" ref="cacheManagerFactory" />
    </bean>
    <!-- 开启缓存对注解的支持 -->
    <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>
    
	<context:component-scan base-package="com.hbzx.dao,com.hbzx.service,com.hbzx.processor,com.hbzx.util,com.hbzx.sdk,com.hbzx.data" /> 
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
	<!-- 引入外部属性文件 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:/conf/db.properties</value>
				<value>classpath:/conf/redis.properties</value>
			</list>
		</property>
	</bean>
    
    <!-- 定义数据源,配置数据库连接池 -->
	<bean id="dataSourceSpied" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="user" value="${hbzx.web.username}" />
		<property name="password" value="${hbzx.web.password}" />
		<property name="driverClass" value="${hbzx.web.driverClassName}" />
		<property name="jdbcUrl" value="${hbzx.web.url}" />
		<property name="maxPoolSize" value="${hbzx.web.maxPoolSize}" />
		<property name="minPoolSize" value="${hbzx.web.minPoolSize}" />
		<property name="acquireIncrement" value="${hbzx.web.acquireIncrement}" />
		<property name="initialPoolSize" value="${hbzx.web.initialPoolSize}" />
		<property name="maxStatements" value="${hbzx.web.maxStatements}" />
		<property name="maxStatementsPerConnection" value="${hbzx.web.maxStatementsPerConnection}" />
		<property name="maxIdleTime" value="${hbzx.web.maxIdleTime}" />
	</bean>
	
	<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
    	<constructor-arg ref="dataSourceSpied" />
  	</bean>
	 
	 <bean id ="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	      <property name = "dataSource" ref="dataSource" />
	 </bean>
	 
	 <!-- 使用JDBC事物 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 使用annotation注解方式配置事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
	 
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxTotal" value="${redis.pool.maxTotal}" />
		<property name="maxIdle" value="${redis.pool.maxIdle}" />
		<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
		<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
		<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />  
    </bean>  
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">  
     <constructor-arg index="0" ref="jedisPoolConfig" /> 
     <constructor-arg index="1" value="${redis.pool.host}" />
     <constructor-arg index="2" value="${redis.pool.port}" />  
     <constructor-arg index="3" value="5000" />  
    </bean>  
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
        <property name="hostName" value="${redis.pool.host}" />  
        <property name="port" value="${redis.pool.port}" />  
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="timeout" value="${redis.timeout}"/>
   	</bean>  
   	<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
       	<property name="connectionFactory"   ref="connectionFactory" />  
   	</bean>   
    <bean id="redisClient" class="com.hbzx.data.client.RedisClient">  
        <property name="redisTemplate" ref="redisTemplate" />  
    </bean>
    <bean id="redisClient2" class="com.hbzx.data.client.RedisClient2">  
        <property name="jedisPool" ref="jedisPool" />  
    </bean>  
    <bean id="categoryWordSegment" class="com.hbzx.data.client.WordSegment"> 
    	<constructor-arg index="0" value="/data/category"/>
    </bean>
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
        <!-- 线程池至少有2个线程是启动的,即使是空闲的也不会关闭 -->
        <property name="corePoolSize" value="2" />  
        <!-- 线程池至少有2个线程是启动的,即使是空闲的也不会关闭 -->
        <property name="maxPoolSize" value="10" />  
        <property name="queueCapacity" value="2000" />  
        <property name="keepAliveSeconds" value="2000" />  
        <property name="rejectedExecutionHandler">  
            <bean class="java.util.concurrent.ThreadPoolExecutor$AbortPolicy" />  
        </property>  
    </bean>
	
    
    <bean id="dnaWishResultDao" class="com.hbzx.dao.DnaWishResultDao">
    	<property name = "redisClient">
	    	<ref local="redisClient"/>
	    </property>
    </bean>
    <bean id="dnaWishResultService" class="com.hbzx.service.DnaWishResultService">
    	<property name="dnaWishResultDao" ref="dnaWishResultDao"></property>
    </bean>
    <bean id="queryClient" class="com.hbzx.data.client.QueryClient">  
    	<!--<constructor-arg index="0" ref="wordSegment" /> 
    	<constructor-arg index="1" value="121.199.25.186" />-->
    	<constructor-arg index="0" value="218.244.129.179" />
        <constructor-arg index="1" value="3312" /> 
    </bean>
	<!--[END]  阿里云Service YYF 2013-08-27-->
	<import resource="classpath*:conf/spring/timeTask.xml"/>
  </beans>  

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧