Java Spring boot基本应用程序:字段说明Repository需要类型为';com.demo.NotesRepository';那是找不到的
TLDR:我正在玩一个简单的Spring启动应用程序,它似乎不起作用,并根据运行方式抛出两种错误: 1) 声称未找到回购bean:Java Spring boot基本应用程序:字段说明Repository需要类型为';com.demo.NotesRepository';那是找不到的,java,spring,spring-boot,Java,Spring,Spring Boot,TLDR:我正在玩一个简单的Spring启动应用程序,它似乎不起作用,并根据运行方式抛出两种错误: 1) 声称未找到回购bean: Field notesRepository in com.demo.service.CreateNoteService required a bean of type 'com.demo.models.NotesRepository' that could not be found. The injection point has the following ann
Field notesRepository in com.demo.service.CreateNoteService required a bean of type 'com.demo.models.NotesRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'learnk8s.io.demo.models.NotesRepository' in your configuration.
2) 因此,我将所有子包转移到包含应用程序类的主包中:
因此,现在取代它的第二个错误是localhost:8080
页面不加载任何内容(404错误)。
主体下方>>>>
整个包结构如下所示:
我的服务代码:
@Slf4j
@Transactional
@Service
public class CreateNoteService implements NoteService {
@Autowired
NotesRepository notesRepository;
@Override
public void saveNote(String description, Model model) {
// Check input
if(description != null && !description.trim().isEmpty()) {
notesRepository.save(new Note(null, description.trim()));
log.info("Saved note successfully");
//After publish you need to clean up the textarea
model.addAttribute("description", "");
}
}
@Override
public void getAllNotes(Model model) {
List<Note> notes = notesRepository.findAll();
Collections.reverse(notes);
model.addAttribute("notes", notes);
}
}
最后,主要应用程序:
@SpringBootApplication
@ComponentScan(basePackages = {"com.demo.controller", "com.demo.models", "com.demo.service"})
public class MainJavaApplication {
public static void main(String[] args) {
SpringApplication.run(MainJavaApplication.class, args);
}
}
我看不出我在确保所有依赖项都正确地自动连接并相应地使用,而且所有依赖项都在同一个主包下时哪里出错了。我是不是遗漏了什么?我在想,既然服务、模型和repo层都在同一个主包中,我甚至不需要添加@ComponentScan
对吗?谢谢
继续:
被标记掉的bean NotesRepository已经被@ComponentScan{…}
通过其包添加,但这似乎也不起作用
编辑2:
我试着将所有包都转移到主应用程序包中,如下所示:
现在spring boot应用程序运行成功,但当我在chrome上运行localhost:8080时,我会看到一个白色标签错误页面:
您的MainJavaApplication看起来像什么?如果未指定要扫描的包,ApplicationContext将无法在启动时创建bean,因此将无法注入 您的MainJavaApplication看起来像什么?如果未指定要扫描的包,ApplicationContext将无法在启动时创建bean,因此将无法注入 只要所有组件都位于
MainJavaApplication
所在位置的子包中,就不需要@ComponentScan
。请将MainJavaApplication
移动到root包中(我猜您的情况是com.demo
)
能否尝试在
MainJavaApplication
中添加@EnableJpaRepositories
?很可能您还需要@EntityScan
。您不需要@ComponentScan
,只要所有组件都位于MainJavaApplication
所在位置的子包中。请将MainJavaApplication
移动到root包中(我猜您的情况是com.demo
)
能否尝试在
MainJavaApplication
中添加@EnableJpaRepositories
?很可能您还需要@EntityScan
。在问题中添加了我的MainJavaApplication类,谢谢!我很好奇,@SpringBootApplication是否已经处理了大规模组件扫描?它已经处理了,但是只有从它所在的位置可以访问的包,我看到您进行了编辑,所以现在应该清楚了,在我的MainJavaApplication类中添加了这个问题,谢谢!我很好奇,@SpringBootApplication是否已经处理了大规模组件扫描?它已经处理了,但是只有从它所在的位置可以访问的包,我看到你进行了编辑,所以现在应该很清楚它现在工作了吗?没有。请参阅编辑2以查看新错误,谢谢!当您尝试通过浏览器获取时,应用程序记录了什么?您现在遇到了一个新问题,与原始问题无关。请就此单独提出一个问题。另外,看起来@MichaelKreutz的建议对您有所帮助。我会考虑他的回答,并选择他的回答作为你的问题的答案。它现在起作用了吗?没有。请参阅编辑2以查看新错误,谢谢!当您尝试通过浏览器获取时,应用程序记录了什么?您现在遇到了一个新问题,与原始问题无关。请就此单独提出一个问题。另外,看起来@MichaelKreutz的建议对您有所帮助。我会考虑他的回答,并选择他的回答作为你的问题的答案。
@Slf4j
@Controller
public class NoteController {
@Autowired
CreateNoteService createNoteService;
@GetMapping("/")
public String index(Model model) {
createNoteService.getAllNotes(model);
return "index";
}
@PostMapping("/note")
public String saveNotes(@RequestParam("image") MultipartFile file,
@RequestParam String description, @RequestParam(required = false) String publish, @RequestParam(required = false) String upload, Model model) throws IOException {
if (publish != null && publish.equals("Publish")) {
createNoteService.saveNote(description, model);
createNoteService.getAllNotes(model);
return "redirect:/";
}
// After save fetch all notes again
return "index";
}
}
@SpringBootApplication
@ComponentScan(basePackages = {"com.demo.controller", "com.demo.models", "com.demo.service"})
public class MainJavaApplication {
public static void main(String[] args) {
SpringApplication.run(MainJavaApplication.class, args);
}
}