当前位置:编程学习 > JAVA >>

Exception in thread "main" java.lang.ClassCastException

总是出现这个问题:求解决方案。
Exception in thread "main" java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to com.ebupt.shlExample.service.UserService

我的xml配置文件:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 <context:annotation-config />
<context:component-scan base-package="com.ebupt.shlExample" />

  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="fileEncoding" value="gbk" />
<property name="locations">
<list>
<value>jdbc.properties</value>
</list>
</property>
</bean>


<bean id="dataSource" 
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"   
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/ebupt/shlExample/pojo/user.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>


主函数:package com.ebupt.shlExample.main;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ebupt.shlExample.service.UserService;

public class UserServiceMain {

private static Logger logger = Logger.getLogger(UserServiceMain.class);

public static void main(String[] args) throws Exception {

ApplicationContext ac = new ClassPathXmlApplicationContext("/beans.xml");
UserService userService = (UserService) ac.getBean("sessionFactory");
logger.info("主函数调用方法");
userService.addUser(null);

}
}

--------------------编程问答-------------------- UserService userService = (UserService) ac.getBean("sessionFactory");
完全看不懂你想干啥啊。。。

用错Hibernate了,网上Google个样例吧。 --------------------编程问答-------------------- 配置文件里都完全没出现SessionFactoryImpl啊,没贴全? --------------------编程问答-------------------- Exception in thread "main" java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to com.ebupt.shlExample.service.UserService

很明显啊!类型转换出错!
UserService userService = (UserService) ac.getBean("sessionFactory");

你取到一个sessionFactory,要转成UserService 不行吧!
这里应该是UserService userService = (UserService) ac.getBean("userService ");吧! --------------------编程问答-------------------- java.lang.ClassCastException: org.hibernate.impl.SessionFactoryImpl cannot be cast to com.ebupt.shlExample.service.UserService


常见异常:  类型不匹配异常

不匹配原因: 强制转换
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,