问题描述
- org.springframework.beans.factory.BeanCreationException
- applicationContext.xml文件
<?xml version=""1.0"" encoding=""UTF-8""?>
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""
xmlns:aop=""http://www.springframework.org/schema/aop""
xsi:schemaLocation=""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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd""><bean id=""optlogger"" class=""com.log.OptLogger""></bean><aop:config> <aop:pointcut expression=""within(com.dao.*)"" id=""servicePointcut""/> <aop:aspect id=""optLoggerAspect"" ref=""optlogger""> <aop:around method=""logger"" pointcut-ref=""servicePointcut""/> </aop:aspect> </aop:config><bean id=""sessionFactory"" class=""org.springframework.orm.hibernate3.LocalSessionFactoryBean""> <property name=""dataSource"" ref=""dataSource""></property> <property name=""mappingResources""> <list> <value>com/entity/User.hbm.xml</value> </list> </property> <property name=""hibernateProperties""> <props> <prop key=""hibernate.dialect""> org.hibernate.dialect.MySQLDialect </prop> </props> </property></bean><bean id=""hibernateuserdao"" class=""com.dao.HibernateUserDao"" init-method=""""> <property name=""sessionfactory"" ref=""sessionFactory""></property></bean></beans>
HibernateUserDao文件
public class HibernateUserDao extends HibernateDaoSupport implements UserDao{
SessionFactory sessionfactory;public SessionFactory getSessionfactory() { return sessionfactory;}public void setSessionfactory(SessionFactory sessionfactory) { this.sessionfactory = sessionfactory;}public int count() { // TODO Auto-generated method stub String sql=""select count(*) from user""; List list=this.getHibernateTemplate().find(sql); return Integer.valueOf(list.get(0).toString());}public void delete(int id) { // TODO Auto-generated method stub User user=findByID(id); this.getHibernateTemplate().delete(user);}public List<User> findAll() { // TODO Auto-generated method stub String sql=""from user""; return this.getHibernateTemplate().find(sql);}public User findByID(int id) { // TODO Auto-generated method stub String sql=""from user where id=?""; List list=this.getHibernateTemplate().find(sqlnew Object[]{id}); if(!list.isEmpty()){ return (User)list.get(0); } return null;}public void save(User user) { // TODO Auto-generated method stub this.getHibernateTemplate().save(user);}public void update(User user) { // TODO Auto-generated method stub this.getHibernateTemplate().update(user);}
}
报错 :Exception in thread ""main"" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateuserdao' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.test.Test1.main(Test1.java:20)
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 12 more