0

次のエラーが表示されます。

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
  @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);
}

私の投稿にはコードが多すぎるため、スタックオーバーフローはテキストを追加しています。このコメントは無視してかまいません。

4

4 に答える 4

2

UserService の実装を提供した方がよいでしょう。

実装に @Service のアノテーションが付けられていることを確認してください。

于 2013-03-08T08:22:02.020 に答える
0

@ComponentScan は、javaConfig でのみ機能します。Javaconfig または xml 構成のいずれかを使用しますが、とにかくサービスを Spring Bean として管理する必要があります。

ソース: http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/ComponentScan.html

于 2013-03-08T09:05:03.230 に答える
0

xml で Bean UserService を作成する必要があります。

または、コントローラがそれを見つけることができません!

于 2013-03-08T08:21:52.407 に答える
0

サービスの実装についてはどうですか? UserService クラスと UserService の実装クラスに @Service アノテーションを付けましたか? Controller で getter&setter をスキップして @Autowired アノテーションをフィールドに設定することもできます。それが機能することはわかっていますが、方法がわかりません。

また、指定したパッケージでこれらのアノテーションを検索するように Spring に指示する必要があります。これが私のやり方です。

    <context:annotation-config/>
    <context:component-scan base-package="your_package"/>
    <mvc:annotation-driven/>
于 2013-03-08T08:22:11.267 に答える