GeeCON Prague 2014*
The moose likes Spring and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Exception in thread "main" org.springframework.beans.factory.BeanCreationException" Watch "Exception in thread "main" org.springframework.beans.factory.BeanCreationException" New topic
Author

Exception in thread "main" org.springframework.beans.factory.BeanCreationException

dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
I Got the following error..................Somebody please help.,,,,I am new to spring and hibernate



Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employee1Dao' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]: Cannot resolve reference to bean 'hibernateTemplate' while setting bean property 'hibernateTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]: Cannot resolve reference to bean 'mydatasource' while setting bean property 'datasource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common.dbcp.BasicDataSource] for bean with name 'mydatasource' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.common.dbcp.BasicDataSource
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTemplate' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]: Cannot resolve reference to bean 'mydatasource' while setting bean property 'datasource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common.dbcp.BasicDataSource] for bean with name 'mydatasource' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.common.dbcp.BasicDataSource
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]: Cannot resolve reference to bean 'mydatasource' while setting bean property 'datasource'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common.dbcp.BasicDataSource] for bean with name 'mydatasource' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.common.dbcp.BasicDataSource
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common.dbcp.BasicDataSource] for bean with name 'mydatasource' defined in file [C:\Users\dell\Desktop\project\spring\SpringHibernate\src\pack1\spring-hibernate.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.common.dbcp.BasicDataSource
Caused by: java.lang.ClassNotFoundException: org.apache.common.dbcp.BasicDataSource
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:201)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:327)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1075)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:346)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:861)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:421)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:156)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
at pack1.SpringHibernateTest.main(SpringHibernateTest.java:22)
My spring-hibernate.xml looks like that

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="mydatasource" class="org.apache.common.dbcp.BasicDataSource">
<property name="driverclassname" value="sun.jdbc.odbc.JdbcOdbcDriver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="datasource" ref="mydatasource" />
<property name="mappingresource">
<list>
<value>/pack1/employee.hbm.xml</value>
</list>
</property>
<property name="hibernateproperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>

<bean id="employee1Dao" class="pack1.EmployeeDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
</beans>

My client application looks like this
package pack1;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class SpringHibernateTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource resource=new FileSystemResource("src/pack1/spring-hibernate.xml");
BeanFactory factory=new XmlBeanFactory(resource);
Employee employee=new Employee();
employee.setAge(23);
employee.setId("12");
employee.setName("Dibakar");
employee.setSalary(1234.78);
EmployeeDao employeeDao=(EmployeeDao)factory.getBean("employee1Dao");
employeeDao.saveOrUpdate(employee);
Employee empresult=employeeDao.getEmployee("123");

System.out.println(empresult);
}
}
My employeeDao class:----
package pack1;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class EmployeeDao {

private HibernateTemplate hibernateTemplate;

public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}


public Employee getEmployee(final String id)
{
HibernateCallback callback=new HibernateCallback() {

@Override
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
return session.load(Employee.class,id);
}
};
return (Employee)hibernateTemplate.execute(callback);
}
public void saveOrUpdate(final Employee employee) {

HibernateCallback callback=new HibernateCallback() {

@Override
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
// TODO Auto-generated method stub
session.saveOrUpdate(employee);
return null;
}
};
hibernateTemplate.execute(callback);
}
}

My other file are employee.java and .hbm.xml which i dont think needed for soloving this issue.somebody please help>>>>>>>>>>>>>>>>
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

The exception is caused by "Caused by: java.lang.ClassNotFoundException: org.apache.common.dbcp.BasicDataSource". Ensure jar with this class is availaible.
dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
Hi Rishi I already added the below jar in the below path
commons-dbcp-1.2.2.jar


\project\spring\SpringHibernate\WebContent\WEB-INF\lib\commons-dbcp-1.2.2.jar
Is it right?
Please let me know incase there is any wrong with that?
dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
Hi as per my previous mail after adding that jar still pgm not working
Rishi Shehrawat
Ranch Hand

Joined: Aug 11, 2010
Posts: 218

How are you executing the unit test, is it command line of from a IDE?
dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
Hi
Yeah i am using Eclispe IDE and right click onthe client application(Spring HibernateTest) and run as a java application.
dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
Hi anyone interested to answer my previous question?
dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
Hi can anyone please tell me the cause:--------
Exception in thread "main" java.lang.ClassCastException: $Proxy1 cannot be cast to org.springframework.orm.hibernate3.HibernateTemplate
at pack1.EmployeeDao$2.doInHibernate(EmployeeDao.java:45)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338)
at pack1.EmployeeDao.saveOrUpdate(EmployeeDao.java:49)
at pack1.SpringHibernateTest.main(SpringHibernateTest.java:28)
dibakar sinha
Greenhorn

Joined: Oct 02, 2011
Posts: 15
please help me solving this issue,
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

It looks like you still are missing jars in the classpath.

Caused by: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver

Is your Derby JDBC driver jar in your classpath.

I highly recommend reading up more on classpath and what it means in terms of having jars and classes in your classpath at compile time and also at runtime.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
GeeCON Prague 2014
 
subject: Exception in thread "main" org.springframework.beans.factory.BeanCreationException