@Autowired注释注入null

时间:2016-07-19 10:11:12

标签: java spring-boot

我正在尝试在DTO类中自动装配CrudRepository但是Spring总是注入null而不是bean。

我的代码如下:

package com.exampleRestApp.task;

import com.exampleRestApp.workers.WorkerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;


public class TaskDTO {

    private int workerID;
    private String name;
    boolean finished;
    private int taskID;

    @Autowired
    WorkerRepository w; //w==null in this moment

public Task MapToEntity() {
    Task task = new Task();
    task.setName(name);
    task.setFinished(finished);
    task.setTaskID(taskID);
    task.setWorker(w.findOne(task.getTaskID()));
    return task;
}

我的工人回复:

package com.exampleRestApp.workers;

import java.util.List;
public interface WorkerRepository extends CrudRepository<Worker,Integer> {
  List<Worker> findBySurname(String surname);
  List<Worker>findAllByOrderByNameAsc();
}

1 个答案:

答案 0 :(得分:1)

您必须使用@Repository@Service spring注释

为您的WorkerRepository添加注释
@Repository
public interface WorkerRepository extends CrudRepository<Worker,Integer>