My app-config.xml có một định nghĩa cho đậu UserDao tôi:Không thể autowire lĩnh vực, nhưng tôi có định nghĩa
<bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
Tôi có quét thành phần của tôi:
<context:component-scan base-package="com.blah" />
action index của tôi trong HomeController của tôi hoạt động tốt (nó xuất ra các nội dung của một phương thức trên UserService của tôi đến một mẫu 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;
}
Các 'getTestUser()' là một phương pháp mới giúp cho tham chiếu đến UserDao, nó trông giống như:
@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();
}
}
Tôi nhận được lỗi:
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)}
- gì vấn đề có thể là gì?
- Nếu tôi không thực hiện autowire, tôi sẽ làm gì thay vì thêm @AutoWire vào định nghĩa UserDao.
ok Tôi đã không triển khai UserDao ahh! cảm ơn! btw, làm thế nào để bạn đọc nhật ký bootstrap của mùa xuân hoặc bộ nhớ thông qua gỡ lỗi?) Đó là những kỹ năng tốt tôi nên học! – Blankman
Tôi đã cập nhật câu trả lời với các tham chiếu đến ghi nhật ký và gỡ lỗi. :) – yclian