我正在使用Spring 4并休眠。有谁能够帮助我!我使用的是不带applicationcontext.xml的批注,但在以前的答案中进行了搜索,但并没有帮助我发生此错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'BookDao' is defined can anybody help me to found the error
INFO : org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@73a8dfcc: startup date [Thu Nov 19 20:28:23 WAT 2015]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
WARN : org.hibernate.mapping.RootClass - HHH000038: Composite-id class does not override equals(): project.model.OrderDetails
WARN : org.hibernate.mapping.RootClass - HHH000039: Composite-id class does not override hashCode(): project.model.OrderDetails
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'BookDao' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1174)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:283)


我的课是

    @Configuration
    @ComponentScan(basePackages ="project.genericdao ,")
    @EnableTransactionManagement
    public class AppConfig {

        @Bean(name = "dataSource")
        public DataSource getDataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("org.postgresql.Driver");
            dataSource.setUrl("jdbc:postgresql://localhost:5432/jeBouquine");
            dataSource.setUsername("postgres");
            dataSource.setPassword("basketball2009");

            return dataSource;
        }
        @Autowired
        @Bean(name = "sessionFactory")
        public SessionFactory getSessionFactory(DataSource dataSource) {

            LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
            sessionBuilder.scanPackages("project.model");
            sessionBuilder.addProperties(getHibernateProperties());
            return sessionBuilder.buildSessionFactory();
        }

        private Properties getHibernateProperties() {
            Properties properties = new Properties();
            properties.put("hibernate.show_sql", "true");
            properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            return properties;
        }
        @Autowired
        @Bean(name = "transactionManager")
        public HibernateTransactionManager getTransactionManager(
                SessionFactory sessionFactory) {
            HibernateTransactionManager transactionManager = new HibernateTransactionManager(
                    sessionFactory);

            return transactionManager;
        }
    }

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import project.model.Book;
    import project.repository.RepositoryBookInterface;
    @Service("bookService")
    public class bookService implements IBookService{
        @Autowired
    private     RepositoryBookInterface bookDao ;

        public void add(Book obj) {
            bookDao.add(obj);

        }


    }

    public class GenericDaoImpl<T> implements GenericDao<T>{

        private Class<T> type;

        //@Resource(name = "sessionFactory")
        @Autowired
        private SessionFactory sessionFactory;
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
        protected SessionFactory getSessionFactory() {
            if (sessionFactory == null)
                throw new IllegalStateException("SessionFactory has not been set on DAO before usage");
            return sessionFactory;
        }


        public void save(T obj) {
            getSessionFactory().getCurrentSession().save(obj);
            getSessionFactory().getCurrentSession().flush();

        }



    @Repository("BookDao")
    public class RepositoryBook implements RepositoryBookInterface{

        private GenericDaoImpl<Book> dao;

        public RepositoryBook() {
            dao = new GenericDaoImpl<Book>(Book.class);
        }

        public void add(Book obj) {
            dao.save(obj);
        }


    }

最佳答案

问题是您@ComponentScan批注。只需将其替换为:

@ComponentScan(basePackages ="project.genericdao")

或简化版:
@ComponentScan("project.genericdao")

应该做到的!

关于java - org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义名为'BookDao'的bean,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33813189/

10-09 00:22