mia app-config.xml ha una definizione per il mio fagiolo UserDao:Impossibile autowire campo, ma ho la definizione
<bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
ho la mia scansione componente:
<context:component-scan base-package="com.blah" />
mia azione index nel mio HomeController funziona bene (restituisce il contenuto di un metodo sul mio UserService a un modello 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;
}
Il 'getTestUser()' è un nuovo metodo che fa riferimento a UserDao, sembra che:
@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();
}
}
sto ottenendo l'errore:
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)}
- Cosa il problema potrebbe essere?
- Se non eseguivo l'autowire, cosa dovrei fare invece di aggiungere @AutoWire nella definizione UserDao.
ok Non stavo implementando la UserDao ahh! Grazie! btw, come leggi il log di bootstrap di primavera o la memoria tramite il debugging?) Quelle sono buone capacità che dovrei imparare! – Blankman
Ho aggiornato la risposta con i riferimenti alla registrazione e al debug. :) – yclian