[英]Required a bean of type Repository that could not be found
I am setting up a new Spring project for some fun learning/tutorial at home and I seem to run into a fairly common issue but I have tried all possible solutions I found on here but with no luck.我正在建立一个新的 Spring 项目,以便在家中进行一些有趣的学习/教程,我似乎遇到了一个相当普遍的问题,但我已经尝试了我在这里找到的所有可能的解决方案,但没有运气。 Basically what I have is as follows:基本上我所拥有的如下:
Controller class: Controller class:
@RestController
@RequestMapping(value = "/shop")
public class ShopController {
@Autowired
ShopService shopService;
@GetMapping(value = "/{id}")
public @ResponseBody Shop getTestData(@PathVariable String id) {
return shopService.getShopBasedOnId(id);
}
}
Service class:服务 class:
@Service
public class ShopService {
@Autowired
private ShopRepository shopRepository;
public ShopService(ShopRepository shopRepository){
this.shopRepository = shopRepository;
}
public Shop getShopBasedOnId(String id) {
return shopRepository.findByShopId(id);
}
}
Repository class:存储库 class:
@Repository
public interface ShopRepository extends PagingAndSortingRepository<Shop, String> {
Shop findByShopId(String shopId);
}
Application class:应用 class:
@SpringBootApplication
@EnableJpaRepositories("com.example.reservations.repository")
public class ReservationsApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationsApplication.class, args);
}
}
Last but not least my pom.xml with the dependencies:最后但并非最不重要的是我的 pom.xml 与依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
So the error code I am getting is as follows:所以我得到的错误代码如下:
Description:
Parameter 0 of constructor in com.example.reservations.services.ShopService required a bean of type 'com.example.reservations.repository.ShopRepository' 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 'com.example.reservations.repository.ShopRepository' in your configuration.
My structure of folders is:我的文件夹结构是:
main
|_java
|_com.example.reservations
|_controllers
|_ShopController.java
|_repository
|_ShopRepository.java
|_services
|_ShopService.java
|_ReservationsApplication.java
The problem could be @Autowired in ShopService问题可能是 ShopService 中的@Autowired
@Service
public class ShopService {
@Autowired
private ShopRepository shopRepository;
public ShopService(ShopRepository shopRepository){
this.shopRepository = shopRepository;
}
public Shop getShopBasedOnId(String id) {
return shopRepository.findByShopId(id);
}
}`
should be @Service public class ShopService {应该是@Service public class ShopService {
private ShopRepository shopRepository;
public ShopService(ShopRepository shopRepository){
this.shopRepository = shopRepository;
}
public Shop getShopBasedOnId(String id) {
return shopRepository.findByShopId(id);
}
}
You can also add @Autowired
just above the constructor but it is no longer required if there is only one constructor您也可以在构造函数上方添加@Autowired
,但如果只有一个构造函数,则不再需要
In my situation, I had spring boot with multiple modules.在我的情况下,我用多个模块启动了 spring。 So I had to the below to make it work.所以我不得不在下面让它工作。
@SpringBootApplication (scanBasePackages={"gov.usda.fsa.fcao.flp.ola"})
@EnableJdbcRepositories(basePackages={"gov.usda.fsa.fcao.flp.ola"})
public class TestOLAApplication {
public static void main(String[] args) {
SpringApplication.run(TestOLAApplication.class, args);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.