我使用弹簧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);
}
}
答案 0 :(得分:1)
问题在于@ComponentScan注释。 Juste将其替换为:
@ComponentScan(basePackages ="project.genericdao")
或简化版:
@ComponentScan("project.genericdao")
应该这样做!