我正在尝试在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();
}
答案 0 :(得分:1)
您必须使用@Repository
或@Service
spring注释
@Repository
public interface WorkerRepository extends CrudRepository<Worker,Integer>