0

I am having an error in my servlet-context.xml. It is saying that it can find the below classes but there actually exist in the projecct folder.

<beans:bean id="personDAO" class="com.springhibernatemvc.dao.PersonDAOImpl">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="personService" class="com.springbibernate.services.PersonServiceImpl">
        <beans:property name="personDAO" ref="personDAO"></beans:property>
    </beans:bean>

It is saying that this class is not found

- Class 'com.springbibernate.services.PersonServiceImpl' 

My servlet-context file is also defined in my web.xml file

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

I have adding servlet-context.xml to the root folder of my web app but still the error exists.

Complete stacktrace

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.springhibernatemvc.dao.PersonDAOImpl] for bean with name 'personDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.springhibernatemvc.dao.PersonDAOImpl
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.springhibernatemvc.dao.PersonDAOImpl] for bean with name 'personDAO' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: com.springhibernatemvc.dao.PersonDAOImpl

What could be wrong?

Blaze
  • 2,269
  • 11
  • 40
  • 82

2 Answers2

1

from Java Docs

Thrown when an application tries to load in a class through its string name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader .
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

The problem here is not with the servlet-context.xml but the beans defined in there. make sure that the full cannonical class names are correct and respective class files are present under WEB-INF or some library under that .

From a first high level look it seems that you are defining bean with class as 'com.springbibernate.services.PersonServiceImpl'

where the correct name appears to be 'com.springhibernate.services.PersonServiceImpl'

So the xml content should be like :

<beans:bean id="personDAO" class="com.springhibernatemvc.dao.PersonDAOImpl">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </beans:bean>
    <beans:bean id="personService" class="com.springhibernate.services.PersonServiceImpl">
        <beans:property name="personDAO" ref="personDAO"></beans:property>
    </beans:bean>
Akash Yadav
  • 2,411
  • 20
  • 32
0

Make an entry of the below tags in your spring-context xml file:

<context:annotation-config />
<context:component-scan base-package="com.springhibernate.services, com.springhibernatemvc.dao" />

if you are getting the java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener then, follow the below link:

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

Community
  • 1
  • 1
khatana
  • 11
  • 3