Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private edu.scramjet.service.AppUserService edu.scramjet.controller.UserController.appUserService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [edu.scramjet.service.AppUserService] 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)}
Solution :
If you look at stack. Spring container is unable to create bean. for userController. unable to autowire the appUserService.
ex : public class void UserController {
@Autowired
private AppUserService appUserService; /// interface object
@RequestMapping(value="/create",method=RequestMethod.POST,
produces=MediaType.APPLICATION_JSON_VALUE,
consumes=MediaType.APPLICATION_JSON_VALUE)
public User createAppUser(@RequestBody User user ){
System.out.println(user);
appUserService.createUser();
return user;
}
}
If AppUserService is object interface and its autowired. We need to provide Implementation class for bean creation.
public interface AppUserService {
public void createUser();
}
@Service
public interface AppUserService implements AppUserServiceImpl {
public void createUser() {
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private edu.scramjet.service.AppUserService edu.scramjet.controller.UserController.appUserService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [edu.scramjet.service.AppUserService] 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)}
Solution :
If you look at stack. Spring container is unable to create bean. for userController. unable to autowire the appUserService.
ex : public class void UserController {
@Autowired
private AppUserService appUserService; /// interface object
@RequestMapping(value="/create",method=RequestMethod.POST,
produces=MediaType.APPLICATION_JSON_VALUE,
consumes=MediaType.APPLICATION_JSON_VALUE)
public User createAppUser(@RequestBody User user ){
System.out.println(user);
appUserService.createUser();
return user;
}
}
If AppUserService is object interface and its autowired. We need to provide Implementation class for bean creation.
public interface AppUserService {
public void createUser();
}
@Service
public interface AppUserService implements AppUserServiceImpl {
public void createUser() {
}
}
No comments:
Post a Comment