我正在使用MongoRepository
和我创建的Service
图层。 @Autowired
功能在我的代码中不起作用,不确定我做错了什么。
服务层
@Service
public class SubmissionsService {
@Autowired
private SubmissionsRepository submissionsRepository;
public List < Submissions > findAll() {
return submissionsRepository.findAll();
}
public List < Submissions > findByUserId(ObjectId id) {
return submissionsRepository.findByUserId(id);
}
public List < Submissions > findByStatus(String status) {
return submissionsRepository.findByStatus(status);
}
public Object insert(Submissions obj) {
return submissionsRepository.insert(obj);
}
}
正如你所看到的那样,我调用了Autowired,但在测试时返回null。
SRP.java
@Service
public class SRP {
@Autowired
private SubmissionsService repository;
public void step1(Submissions obj) {
System.out.println(repository);
}
}
存储库
public interface SubmissionsRepository extends MongoRepository < Submissions, String > {
public List < Submissions > findAll();
public List < Submissions > findByUserId(ObjectId userId);
public List < Submissions > findByStatus(String status);
// public void save(Submissions object);
}
配置文件
@SpringBootApplication
@Controller
public class UiApplication {
public static void main(String[] args) {
SpringApplication.run(UiApplication.class, args);
new SRP().step1();
}
}