0
votes

I am getting this error: Error:Error creating bean with name 'breadController': Unsatisfied dependency expressed through field 'breadDAOImpl'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.DaoImpl.BreadDAOImpl' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

BreadController.java

    package com.niit.controllers;
    import java.util.List;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    import com.DaoImpl.BreadDAOImpl;
    import com.model.Bread;

    @Controller
    public class BreadController {

        @Autowired
        public BreadDAOImpl breadDAOImpl;

        @RequestMapping("/bread")
        public ModelAndView getAllUsers()
        {
             List<Bread>ll=breadDAOImpl.getAllBread();
             ModelAndView ss=new ModelAndView("bread","allbread",ll);
             return ss;
        }

    }

BreadDAO.java

    package com.Dao;
    import java.util.List;

    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;

    import com.model.Bread;
    @Repository("BreadDAO")
    public interface BreadDAO {


        public List<Bread> getAllBread();

    }

BreadDAOImpl.java

    package com.DaoImpl;
    import java.util.*;
    import java.util.logging.Logger;

    import javax.transaction.Transaction;
    import javax.transaction.Transactional;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    import org.springframework.stereotype.Service;

    import com.Dao.BreadDAO;
    import com.model.Bread;

    @Repository
    public class BreadDAOImpl implements BreadDAO {
        @Autowired
        SessionFactory sessionFac;
        //List<Bread> breads;


        @SuppressWarnings("unchecked")
        public  List<Bread> getAllBread() {
            Session session = sessionFac.openSession();
            session.beginTransaction();
            List<Bread> breadlist = session.createQuery("from Bread").list();
            session.getTransaction().commit();
            session.close();
            return breadlist;

        }

        public BreadDAOImpl(SessionFactory sessionFactory)
        {
            super();
            sessionFac=sessionFactory;

        }   


    }

Hiber Config:

    package com.hibernateConfig;
    import java.util.*;
    import javax.sql.DataSource;
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.orm.hibernate4.HibernateTransactionManager;
    import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import com.model.*;
    import com.Dao.*;
    import com.DaoImpl.BreadDAOImpl;

    @Configuration
    @ComponentScan("com.niit")
    @EnableTransactionManagement
    public class HibernateConfig {
        @Bean(name="datasource")
        public DataSource getH2() {
            System.out.println("Hibernate initiated");
            DriverManagerDataSource dt=new DriverManagerDataSource();
            dt.setDriverClassName("org.h2.Driver");
            dt.setUrl("jdbc:h2:~/test");
            dt.setUsername("sa");
            dt.setPassword("");
            System.out.println("conncection es");
            return dt;
        }
        private Properties getHiberProps(){
            Properties p=new Properties();
            p.put("hibernate.dialect","org.hibernate.dialect.H2Dialect");
            p.put("hibernate.show_sql","true");
            p.put("hibernate.hbm2ddl.auto", "update");
            return p;
        }
        @Autowired
        @Bean(name="sessionFactory")
        public SessionFactory getSession(DataSource datasource) {
            LocalSessionFactoryBuilder lsfb=new LocalSessionFactoryBuilder(datasource);
            lsfb.addProperties(getHiberProps());
            lsfb.addAnnotatedClass(Bread.class);
            return lsfb.buildSessionFactory();
        }   
        @Autowired
        @Bean(name="transactionManager")
        public HibernateTransactionManager getTransaction(SessionFactory sessionFactory) {

            HibernateTransactionManager tm=new HibernateTransactionManager(sessionFactory);
            return tm;
        }

        @Autowired
        @Bean("name=BreadDAO")
        public BreadDAO getBreadDAO(SessionFactory sessionFac)

        {
            System.out.println("BreadDAO object creation");
            return new BreadDAOImpl(sessionFac);


        }
    }
1

1 Answers

0
votes

In your @Configuration class, you defined the BreadDAO in the following way:

@Autowired
@Bean("name=BreadDAO")
public BreadDAO getBreadDAO(SessionFactory sessionFac) {
    System.out.println("BreadDAO object creation");
    return new BreadDAOImpl(sessionFac);
}

This definition exposes a BreadDAO bean type named BreadDAO.

To have this bean injected in your BreadController you have to change the definition of the property from BreadDAOImpl to BreadDAO:

@Autowired
public BreadDAO breadDAO;