gpt4 book ai didi

java - org.springframework.beans.factory.NoSuchBeanDefinitionException : No bean named 'BookDao' is defined

转载 作者:行者123 更新时间:2023-11-30 07:46:47 25 4
gpt4 key购买 nike

我正在使用 spring 4 和 hibernate。有谁能够帮助我!我只使用注释,没有 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)

我的类(class)是

    @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 : No bean named 'BookDao' is defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com