[英]SpringBoot Application cannot find defined Repository in Controller
我正在按照本指南将MySql添加到已存在的SpringBoot项目中,该项目的依赖项管理处于缓慢状态。 就在我添加本教程中使用的这三个类时,如下所示
main / java / net / code / model / Users.Java
package net.code.controller;
import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
用户存储库为main / java / net / code / repo / UserRepository.Java包net.code.repo;
import net.code.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}
使用Web服务控制器main / java / net / code / controller / MainController.Java
package net.code.controller;
import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@GetMapping(path="/add") // Map ONLY GET Requests
public @ResponseBody String addNewUser (@RequestParam String name
, @RequestParam String email) {
// @ResponseBody means the returned String is the response, not a view name
// @RequestParam means it is a parameter from the GET or POST request
User n = new User();
n.setName(name);
n.setEmail(email);
userRepository.save(n);
return "Saved";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
我的@SpringBoot类main / java / net / code / App.Java
package net.code;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//@CrossOrigin(origins = "http://127.0.0.1:8080")
@SpringBootApplication
@ComponentScan("net.code")
//@ComponentScan(basePackages = { "net.code","net.code.repo"})
//@RestController
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class App extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
但是无论何时运行该应用程序,我都会不断收到以下消息
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-10-21 15:11:59.674 ERROR 67424 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in net.code.controller.MainController required a bean of type 'net.code.repo.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'net.code.repo.UserRepository' in your configuration.
Process finished with exit code 1
我寻找像这些相关的问题春天启动不自动装配@Repository , @RestController在其他包不能正常工作 ,但无法修复的那些链接的建议,我没有工作,我也想尝试接受的解决方案在这里考虑在您的配置中定义了“包”类型的bean [Spring-Boot],但是我发现@EnableJpaRepositories没有这样的包
请帮我解决这个问题,因为我已经尝试了好几天了
这段代码的完美工作版本在github上 。 您可以检查http://start.spring.io以获得spring-data-jpa的相应gradle版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.