【发布时间】:2011-03-17 17:03:42
【问题描述】:
我的 app-config.xml 有我的 UserDao bean 的定义:
<bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
我正在扫描我的组件:
<context:component-scan base-package="com.blah" />
我的 HomeController 中的索引操作工作正常(它将我的 UserService 上的方法的内容输出到 freemarker 模板)。
@Controller
public class HomeController {
@Autowired
private UserService userService;
@RequestMapping("/")
public ModelAndView Index() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mav.addObject("message", userService.sayHello());
mav.addObject("username", userService.getTestUser());
return mav;
}
'getTestUser()' 是一个引用 UserDao 的新方法,它看起来像:
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserDao userDao;
public String sayHello() {
return "hello from user service impl part 2";
}
public String getTestUser() {
return userDao.getById(1L).getUsername();
}
}
我收到错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.blah.core.db.hibernate.UserDao com.blah.core.services.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.blah.core.db.hibernate.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
- 可能是什么问题?
- 如果我不执行自动装配,我会做什么而不是在 UserDao 定义中添加 @AutoWire。
【问题讨论】:
标签: java hibernate spring spring-mvc