2013-03-08 68 views
0

我得到这个错误:自动装配Autowired在春天给了一个错误:自动装配依赖注射失败

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'newStep2Controller': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void projecthealth.web.NewStep2Controller.setUserService(projecthealth.service.UserService); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [projecthealth.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) 
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) 

Controller类

@Controller 
    @RequestMapping("/newStep2.htm") 
    @SessionAttributes("user") 
    @ComponentScan("projecthealth.service") 
public class NewStep2Controller 
{ 
protected final Log logger = LogFactory.getLog(getClass()); 
private UserService userService; 

@Autowired 
public void setUserService(UserService userService) { 
    this.userService = userService; 
} 
    @RequestMapping(method = RequestMethod.GET) 
public String showUserForm(ModelMap model) 
{ 
    model.addAttribute("user"); 

    return "userForm"; 
} 

服务现有:

public interface UserService { 

void createUser(User user) throws ServiceException; 

/** 
* 
* @param userId (email is user id) 
* @return 
* @throws ServiceException 
*/ 
User getUserById(String userId) throws ServiceException; 

void deleteUser(String userId) throws ServiceException; 

/** 
* 
* @param newUserObject 
* @param userId (email is user id) 
* @return 
* @throws ServiceException 
*/ 
User updateUser(User newUserObject, String userId) throws ServiceException; 
} 

我已将此添加到xml

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 

我已经添加了UserServiceImpl

public class UserServiceImpl extends BaseServiceImpl<User> implements UserService{ 

public static final String FIELD_EMAIL = "email"; 

public void createUser(User user) throws ServiceException { 
    insert(user); 
} 

public User getUserById(String userId) throws ServiceException { 
    return (User) findOne(User.class, FIELD_EMAIL, userId); 
} 

public void deleteUser(String userId) throws ServiceException { 
    delete(User.class, FIELD_EMAIL, userId); 
} 

public User updateUser(User newUserObject, String oldEmail) throws ServiceException { 
    MongoTemplate template = getTemplate(); 
    User userObject = getUserById(oldEmail); 

    List<DietCategory> dietaryPreferences = newUserObject.getDietaryPreferences(); 

    if(dietaryPreferences != null){ 
     userObject.setDietaryPreferences(dietaryPreferences); 
    } 
    userObject.setEmail(newUserObject.getEmail()); 
    userObject.setFirstname(newUserObject.getFirstname()); 
    userObject.setHeight(newUserObject.getHeight()); 
    userObject.setLastname(newUserObject.getLastname()); 
    userObject.setPassword(newUserObject.getPassword()); 
    userObject.setWeight(newUserObject.getWeight()); 
    template.save(userObject); 
    return newUserObject; 
} 

public List<User> getAllUser() throws ServiceException { 
    return findAll(User.class); 
} 

计算器正在添加更多的文本,因为在我的帖子太多的代码。 你可以忽略这个评论。

+1

你的界面的实现在哪里? Spring必须实例化一些东西,而且它不能成为接口。 – partlov 2013-03-08 08:22:08

回答

0

您需要在xml中创建一个bean UserService。

或者你的控制器找不到它!

2

如果您提供了UserService的实现,那会更好。

确保使用@Service对实现进行注释。

+0

所以上面,我有UserSerivceImpl,我应该包括@Service上面的行“公共类UserServiceImpl扩展BaseServiceImpl”? – 2013-03-11 23:08:13

0

如何实施您的服务?您是否使用@Service注释标注了UserService类和UserService的实现类? 你也可以在Controller中跳过getter & setter,并将@Autowired注解设置为你的字段,我知道它可行,但我不知道如何。

你还指示春天来搜索指定的包中的这些注释,这是我该怎么做:

<context:annotation-config/> 
    <context:component-scan base-package="your_package"/> 
    <mvc:annotation-driven/> 
+0

我已经在我的xml中有这个了。 – 2013-03-11 23:10:57

相关问题