我正在建立一个新的 Spring 项目的一些有趣的学习/教程在家里,我似乎遇到了一个相当常见的问题,但我已经尝试了所有可能的解决方案,我发现在这里,但没有运气。基本上我有如下:
控制器类别:
@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
public class ShopService {
@Autowired
private ShopRepository shopRepository;
public ShopService(ShopRepository shopRepository){
this.shopRepository = shopRepository;
}
public Shop getShopBasedOnId(String id) {
return shopRepository.findByShopId(id);
}
}
存储库类:
@Repository
public interface ShopRepository extends PagingAndSortingRepository<Shop, String> {
Shop findByShopId(String shopId);
}
应用程序类别:
@SpringBootApplication
@EnableJpaRepositories("com.example.reservations.repository")
public class ReservationsApplication {
public static void main(String[] args) {
SpringApplication.run(ReservationsApplication.class, args);
}
}
最后但并非最不重要的是,我的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>
因此,我得到的错误代码如下:
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.
我的文件夹结构是:
main
|_java
|_com.example.reservations
|_controllers
|_ShopController.java
|_repository
|_ShopRepository.java
|_services
|_ShopService.java
|_ReservationsApplication.java
3条答案
按热度按时间cgvd09ve1#
原来我必须有一个配置文件的沙发库。我所做的是遵循以下link,现在它都编译好!
zd287kbt2#
问题可能是@在商店服务中自动布线
应该是@服务公共类购物服务{
您还可以在构造函数的正上方添加
@Autowired
,但如果只有一个构造函数,则不再需要它toiithl63#
在我的情况下,我有多个模块的 Spring Boot 。所以我必须在下面使它工作。