我有像这样的Hibernate配置设置
@Configuration
@PropertySource("classpath:db.properties")
@ComponentScan("com.fyt.fyt")
@EnableTransactionManagement
public class ApplicationContextConfig {
@Autowired
private Environment env;
@Bean()
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/views/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
@Bean()
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}
@Autowired
@Bean()
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(getDataSource());
Properties props = new Properties();
props.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
props.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
factoryBean.setHibernateProperties(props);
factoryBean.setAnnotatedClasses(User.class);
return factoryBean;
}
@Autowired
@Bean()
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
@Autowired
@Bean()
public UserDao getUserDao(SessionFactory sessionFactory) {
return new UserDaoImpl(sessionFactory);
}
}
我有像这样的UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Transactional
@Override
public List<User> list() {
return userDao.list();
}
@Transactional
@Override
public User getUser(int id) {
return userDao.getUser(id);
}
@Transactional
@Override
public void saveOrUpdate(User user) {
userDao.saveOrUpdate(user);
}
@Transactional
@Override
public void delete(int id) {
userDao.delete(id);
}
}
和UserDaoImpl.java一样
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private SessionFactory sessionFactory;
public UserDaoImpl() {
}
public UserDaoImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<User> list() {
List<User> users = (List<User>)sessionFactory.getCurrentSession().createCriteria(User.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return users;
}
@Override
public User getUser(int id) {
String hql = "from tbl_user where id="+id;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
List<User> users = (List<User>)query.list();
if(!users.isEmpty() && users != null) {
return users.get(0);
}
return null;
}
@Override
public void saveOrUpdate(User user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
@Override
public void delete(int id) {
User usertodelete = new User();
usertodelete.setId(id);
sessionFactory.getCurrentSession().delete(usertodelete);
}
}
当我运行这个时,我得到了这个异常
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.fyt.fyt.daos.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=userDao)}
我在UserDaoImpl中定义@Repository,在UserServiceImpl中定义@Serice,我也在进行组件扫描,如@ComponentScan(“com.fyt.fyt”)“com.fyt.fyt”是根级别包,但仍然,我收到这个错误。我做错了什么?
答案 0 :(得分:3)
getUserDao()
必须为userDao()
。@Qualifier(value=getUserDao)
UserDao
)使用@Bean
注释的方法的名称将成为bean的名称。所以目前bean名称是“getUserDao”,而你试图在你的HomeController中自动装配名为“userDao”的bean