1

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.home.dao.ProductDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

package com.home.dao;

 @Repository
 @Transactional
 @Service
 public class ProductDao implements proDaoInterface{


@Autowired
private SessionFactory sessionFactory;


public void addProduct(ProductModel product)
{
    Session session= sessionFactory.getCurrentSession();
    session.saveOrUpdate(product);
    session.flush();

}

public ProductModel getProductByID(String ID)
{
    Session session = sessionFactory.getCurrentSession();
    ProductModel product = (ProductModel)session.get(ProductModel.class,ID);            
    session.flush();
    return product;
}

@SuppressWarnings("unchecked")
public List<ProductModel> getProductList()
{

    Session session= sessionFactory.getCurrentSession();
    List<ProductModel> listProduct; 
    listProduct = session.createQuery("from ProductModel").getResultList();
    session.flush();
    return listProduct;
}

public void deleteProuct(String ID)
{
    Session session = sessionFactory.getCurrentSession();
    session.delete(getProductByID(ID));
    session.flush();

}
}

controller class

  @EnableWebMvc
  @Controller
  public class homeController {
    @Autowired
    private ProductDao dao;

   @RequestMapping("/test")
    public String home(Model model) {
    List<ProductModel> productList = dao.getProductList();
    model.addAttribute("productList", productList);
    return "home";

   }
    @RequestMapping("/view/{productID}")
    public String viewProduct(@PathVariable String productID, Model model) throws IOException {
    ProductModel product = dao.getProductByID(productID);
    model.addAttribute(product);

    return "viewProduct";

 }

  }

Spring Configuration file

  <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"
        value="oracle.jdbc.driver.OracleDriver"></property>
    <property name="url"
        value="jdbc:oracle:thin:@localhost:1521:xe"></property>
    <property name="username" value="system"></property>
    <property name="password" value="admin"></property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hbm2ddl.auto">update</prop>
            <prop key="dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="connection.pool_size">1</prop>
            <prop key="show_sql">true</prop>
        </props>
    </property>

    <property name="packagesToScan">
        <list>
            <value>com.home.controllers</value>
            <value>com.home.dao</value>
            <value>com.home.model</value>
        </list>
        </property>
   </bean>

<bean id="hibernateTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

Dispatcher -Servlet.xml

<context:component-scan base-package="com.home.controllers"></context:component-scan>

<mvc:annotation-driven/>
<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/view/" />
    <property name="suffix" value=".jsp" />
</bean>

  <mvc:resources mapping="/resources/**"    location="/WEB-INF/resources/" />
  <mvc:resources mapping="/images/**"   location="/WEB-INF/resources/images/" />
  <mvc:resources mapping="/css/**"      location="/WEB-INF/resources/css/" />
  <mvc:resources mapping="/fonts/**"        location="/WEB-INF/resources/fonts/" />
  <mvc:resources mapping="/js/**"       location="/WEB-INF/resources/js/" />
  <mvc:resources mapping="/view/**"       location="/view/" />
  <tx:annotation-driven />
</beans>

web

    <display-name>Product</display-name>
    <listener>
        <listener-class> 
           *org.springframework.web.context.ContextLoaderListener*
            </listener-class>
       </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/ApplicationConfig.xml,
        /WEB-INF/DispatcherServlet.xml
        </param-value>
      </context-param>
   <servlet>
    <servlet-name>dad-frontController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

 <servlet-mapping>
    <servlet-name>dad-frontController</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 </web-app>
ABHISHEK
  • 57
  • 6

1 Answers1

0

I believe your error is coming in fact you are restricting your component scan on dispatcher-servlet.xml.

Change the following:

<context:component-scan base-package="com.home.controllers"></context:component-scan>

To:

<context:component-scan base-package="com.home.*"></context:component-scan>

Checkout the official doc: https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-annotation-config

One more thing, your ProductDao has @Service and @Repository. Both say the same to Spring, so leave only @Repository.

Besides that, I would advise you to use Spring Boot, or, make those configurations via programmatically, it's way more clear =)

Bruno
  • 2,889
  • 1
  • 18
  • 25