总是出现这个问题:求解决方案。
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); }
}
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); }
}
解决方案 »
- SSH整合问题,HinernateUserDAO里的方法没有执行?
- 用Java写一个监听器
- hibernate映射问题
- 在SS+ibatis中对象引用问题,小白发贴。
- 有人居然通过 jre1.5 中的 jucheck.exe文件入侵我的系统?!!
- hibernate 查询 多个count如何取出
- myeclipse下的struts+hibernate问题.
- 如何保留两位小数?
- ===急救:为什么远程接口可以用,本地接口就不行??====
- springmvc抛弃父上下文,做到事务在各层中,被扫描的bean被加载了两次、
- Head First Servlet JSP第13章响应过滤器疑问
- java中怎么获取客户端的真实的IP
完全看不懂你想干啥啊用错Hibernate了,网上Google个样例吧。
UserService userService = (UserService) ac.getBean("sessionFactory");你取到一个sessionFactory,要转成UserService 不行吧!
这里应该是UserService userService = (UserService) ac.getBean("userService ");吧!
常见异常: 类型不匹配异常不匹配原因: 强制转换