没有实现models.UserDAO的绑定。找到models.UserDAO

时间:2016-06-08 10:34:22

标签: java spring-mvc playframework

我正在使用play创建示例应用程序。

我在运行时遇到这些异常:

1) No implementation for models.UserDAO was bound.
      while locating models.UserDAO
        for parameter 0 at services.impl.UserServiceImpl.<init>(UserServiceImpl.java:24)
      while locating services.UserService
        for parameter 0 at controllers.UserController.<init>(UserController.java:26)
      while locating controllers.UserController
        for parameter 4 at router.Routes.<init>(Routes.scala:44)
      while locating router.Routes
      while locating play.api.inject.RoutesProvider
      while locating play.api.routing.Router
        for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
      while locating play.api.http.JavaCompatibleHttpRequestHandler
      while locating play.api.http.HttpRequestHandler
        for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
      at play.api.DefaultApplication.class(Application.scala:221)
      while locating play.api.DefaultApplication
      while locating play.api.Application

任何人都可以帮我解决这个问题。

我的课程在下面,

UserDAO.java

@Named
@Singleton
public interface UserDAO extends CrudRepository<User, Long> {

    @Query("select userId,firstName from User where userId =:userId")
    public List<Object[]> getUserDetails(@Param("userId") Integer userId);
}

UserController.java

@Named
@Singleton
public class UserController extends Controller {

    private final UserService userService;

    @Inject
    public UserController(final UserService userService) {
        this.userService = userService;
    }

    public Result getUserDetails(Integer userId) {

        JsonNode output = Json.toJson(userService.getUserDetails(userId));
        return ok(output);
    }
}

UserServiceImpl.java

@Named
@Singleton
public class UserServiceImpl implements UserService {

    private final UserDAO userDAO;

    @Inject
    public UserServiceImpl(final UserDAO userDAO)
    {
        this.userDAO=userDAO;
    }

    public MainDTO getUserDetails(Integer userId)
    {
        List<Object[]> query=userDAO.getUserDetails(userId);
        List<UserDTO> list= new LinkedList<UserDTO>();
        UserDTO userDTO=new UserDTO();
        MainDTO mainDTO=new MainDTO();

        for(Object[] rows : query)
        {
            Integer Id = (Integer)rows[0];
            String name = (String)rows[1];

            userDTO.setNumber(Id);
            userDTO.setName(name);
            list.add(userDTO);
        }

        mainDTO.setData(list);

        return mainDTO;

    }
}

请帮我找出上述问题的确切原因。

先谢谢。

1 个答案:

答案 0 :(得分:0)

你应该自动装配或注射它。

@Autowired
private final UserDAO userDAO;

并在servlet-dispatcher.xml

 <bean id="UserDAO " class="com.dao.UserDAOImpl"></bean>

你也可以在构造函数中使用@Qualifier

@Autowired(required=false)
public UserController(@Qualifier("UserServiceImpl") final UserService userService) {
    this.userService = userService;
}