Announcement Announcement Module
Collapse
No announcement yet.
HIbernate integration java.lang.ClassNotFoundException: CategoryDaoImpl Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HIbernate integration java.lang.ClassNotFoundException: CategoryDaoImpl

    HI there here is my code

    Code:
    <?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:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    	<!-- Scans within the base package of the application for @Components to configure as beans -->
    	<!-- @Controller, @Service, @Configuration, etc. -->
    	<context:component-scan base-package="com.app" />
    
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<mvc:annotation-driven />
    	
    	<context:annotation-config />
    	
        <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
        
        
    	
    	<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    	    <property name="definitions">
    	        <list>
    	            <value>/WEB-INF/views/frontend/layout/frontend.xml</value>
    	        </list>
    	    </property>
    	</bean>
    
    	<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles2.TilesView
            </value>
        </property>
        <property name="order" value="0"/>
    	</bean>
    	
    	<bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	    <property name="viewClass"
    	       value="org.springframework.web.servlet.view.JstlView" />
    	    <property name="prefix" value="/WEB-INF/views/" />
    	    <property name="suffix" value=".jsp" />
    	    <property name="order" value="1"/>
    	</bean>
    
    	<mvc:resources location="/images/" mapping="/images/**"/>
        <mvc:resources location="/styles/" mapping="/styles/**"/>
        
    
    	<context:property-placeholder location="WEB-INF/jdbc.properties" />
    	<bean id="dataSource"
    	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${database.driver}" />
    		<property name="url" value="${database.url}" />
    		<property name="username" value="${database.user}" />
    		<property name="password" value="${database.password}" />
    	</bean>
    	
    	<bean id="categoryDao" class="CategoryDaoImpl">
            <property name="categoryDao" ref="categoryDao"/>
        </bean>
    	
    	<bean id="serviceI" class="ServiceImp">
            <property name="serviceI" ref="serviceI"/>
        </bean>
    	
    	<bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>com.app.model.Category</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    			</props>
    		</property>
    	</bean>
    	
    	
    	<bean id="hibernatetransactionManager"
    	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>
    	
    </beans>

    Code:
    package com.app.dao;
    
    import java.util.List;
    
    import com.app.model.Category;
    
    public interface CategoryDao {
    	
    		  // To Save the article detail
    		  public void saveCategory ( Category category );
    		  
    		  public Category getCategoryById ( Integer id);
    		  
    		  // To get list of all articles
    		  public List<Category> listCategory();
    
    }
    Code:
    package com.app.dao;
    
    import java.util.Date;
    import java.util.List;
    
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;
    
    import com.app.model.Category;
    
    @Component
    @Repository
    public class CategoryDaoImpl {
    
    
    	  @Autowired
    	  private SessionFactory sessionFactory;
    	  
    
    	  // To Save the article detail
    	  public void saveCategory(Category category) {
    	    category.setAddedDate(new Date());
    	    sessionFactory.getCurrentSession().saveOrUpdate(category);
    	  }
    	  
    	  public Category getCategoryById(Integer id) {
    		  return (Category) sessionFactory.getCurrentSession().createQuery(" select * from category where id = "+id);
    	  }
    	  
    	  // To get list of all articles
    	  @SuppressWarnings("unchecked")
    	  public List<Category> listCategorys() {    
    	    return (List<Category>) sessionFactory.getCurrentSession().createCriteria(Category.class).list();
    	  }
    
    }

    Code:
    package com.app.service;
    
    import java.lang.annotation.Annotation;
    import java.util.List;
    
    import org.springframework.transaction.annotation.Transactional;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.app.dao.CategoryDao;
    import com.app.model.Category;
    
    @Service
    public class ServiceImp implements ServiceInterface {
    
      @Autowired(required=true)
      private CategoryDao categoryDao;
    
      public ServiceImp() {
      }
      @Transactional
      public Category getCategoryById(Integer id) {
    	  return categoryDao.getCategoryById(id);
      }
      @Transactional
      public List<Category> listCategory() {
        return categoryDao.listCategory();
      }
    
    
    }

    Code:
    package com.app.controller;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.app.service.ServiceInterface;;
    
    @Component
    @Controller
    @RequestMapping(value = "/")
    public class Controller1 {
    	
    	@Autowired
    	 private ServiceInterface serviceI;
    
    	   @RequestMapping(value = "")
    	    public ModelAndView home() {
    		    Map<String, Object> model = new HashMap<String, Object>();
    	        System.out.println("HomeController: Passing through...");
    	        model.put("category",  serviceI.getCategoryById(1));
    	        return new ModelAndView("frontend/home", model);
    	    }
    	   
    }

  • #2
    Here is the error

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'controller1': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private com.app.service.ServiceInterface com.app.controller.Controller1.serviceI; nested exception is org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [CategoryDaoImpl] for bean with name 'categoryDao' defined in ServletContext resource [/WEB-INF/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: CategoryDaoImpl
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 287)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:464)
    at org.springframework.web.servlet.FrameworkServlet.c onfigureAndRefreshWebApplicationContext(FrameworkS ervlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:5 88)
    at org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:6 45)
    at org.springframework.web.servlet.FrameworkServlet.i nitWebApplicationContext(FrameworkServlet.java:508 )
    at org.springframework.web.servlet.FrameworkServlet.i nitServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.in it(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.j ava:212)
    at org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:1173)
    at org.apache.catalina.core.StandardWrapper.load(Stan dardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnSta rtup(StandardContext.java:4421)
    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4734)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(Sta ndardService.java:525)
    at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalin a.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:414)
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private com.app.service.ServiceInterface com.app.controller.Controller1.serviceI; nested exception is org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [CategoryDaoImpl] for bean with name 'categoryDao' defined in ServletContext resource [/WEB-INF/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: CategoryDaoImpl
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:506)
    at org.springframework.beans.factory.annotation.Injec tionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 284)
    ... 34 more
    Caused by: org.springframework.beans.factory.CannotLoadBeanCl assException: Cannot find class [CategoryDaoImpl] for bean with name 'categoryDao' defined in ServletContext resource [/WEB-INF/servlet-context.xml]; nested exception is java.lang.ClassNotFoundException: CategoryDaoImpl
    at org.springframework.beans.factory.support.Abstract BeanFactory.resolveBeanClass(AbstractBeanFactory.j ava:1262)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.predictBeanType(Abstrac tAutowireCapableBeanFactory.java:576)
    at org.springframework.beans.factory.support.Abstract BeanFactory.isFactoryBean(AbstractBeanFactory.java :1331)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanNamesForType(DefaultList ableBeanFactory.java:317)
    at org.springframework.beans.factory.BeanFactoryUtils .beanNamesForTypeIncludingAncestors(BeanFactoryUti ls.java:185)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.findAutowireCandidates(DefaultL istableBeanFactory.java:833)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.doResolveDependency(DefaultList ableBeanFactory.java:790)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.resolveDependency(DefaultListab leBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredFieldElem ent.inject(AutowiredAnnotationBeanPostProcessor.ja va:478)
    ... 36 more
    Caused by: java.lang.ClassNotFoundException: CategoryDaoImpl
    at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadC lass(WebappClassLoader.java:1526)
    at org.springframework.util.ClassUtils.forName(ClassU tils.java:257)


    I tried everything. I have been working on this for long time. I am new to spring.
    Thank you for your time.

    Comment

    Working...
    X