创建名为'breadController'的bean时出错:通过字段'breadDAOImpl'表示不满意的依赖关系

时间:2017-10-08 12:05:10

标签: java spring hibernate maven spring-mvc

我收到此错误:错误:创建名为'breadController'的bean时出错:通过字段'breadDAOImpl'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'com.DaoImpl.BreadDAOImpl'类型的限定bean可用:预期至少有1个bean可以作为autowire候选者。依赖注释:{@ 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配置:

    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 个答案:

答案 0 :(得分:0)

@Configuration课程中,您通过以下方式定义了 BreadDAO

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

此定义公开名为 BreadDAO BreadDAO bean类型。

要在BreadController中注入此bean,您必须将属性的定义从BreadDAOImpl更改为BreadDAO

@Autowired
public BreadDAO breadDAO;