当前位置: 首页 > 知识库问答 >
问题:

创建名为“bread controller”的bean时出错:通过字段“bread daoimpl”表示的依赖项不满足

贺轶
2023-03-14
    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;
        }

    }
    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();

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

贺宝
2023-03-14

@configuration类中,您以以下方式定义了面包道:

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

这个定义公开了一个名为breaddao的breaddaobean类型。

要将此bean注入breadController中,必须将属性的定义从breaddaoimpl更改为breaddao:

@Autowired
public BreadDAO breadDAO;
 类似资料: