【发布时间】:2022-01-09 01:06:00
【问题描述】:
尝试运行 Spring Boot 应用程序时出现以下错误。
***************************应用程序启动失败
说明:
com.javanovice.crud.example.service.ProductService 中的字段存储库 需要一个 bean 类型 'com.javanovice.crud.example.repository.ProductRepository' 可以 找不到。
注入点有如下注解:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑定义一个 bean 类型 'com.javanovice.crud.example.repository.ProductRepository' 在你的 配置。
ProductRepository.java
package com.javanovice.crud.example.repository;
import com.javanovice.crud.example.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends JpaRepository<Product,Integer> {
Product findByName(String name);
}
ProductService.java
package com.javanovice.crud.example.service;
import com.javanovice.crud.example.entity.Product;
import com.javanovice.crud.example.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public Product saveProduct(Product product) {
return repository.save(product);
}
public List<Product> saveProducts(List<Product> products) {
return repository.saveAll(products);
}
public List<Product> getProducts() {
return repository.findAll();
}
public Product getProductById(int id) {
return repository.findById(id).orElse(null);
}
public Product getProductByName(String name) {
return repository.findByName(name);
}
public String deleteProduct(int id) {
repository.deleteById(id);
return "product removed !! " + id;
}
public Product updateProduct(Product product) {
Product existingProduct = repository.findById(product.getId()).orElse(null);
existingProduct.setName(product.getName());
existingProduct.setQuantity(product.getQuantity());
existingProduct.setPrice(product.getPrice());
return repository.save(existingProduct);
}
}
ProductController.java
package com.javanovice.crud.example.controller;
import com.javanovice.crud.example.entity.Product;
import com.javanovice.crud.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class ProductController {
@Autowired
private ProductService service;
@PostMapping("/addProduct")
public Product addProduct(@RequestBody Product product) {
return service.saveProduct(product);
}
@PostMapping("/addProducts")
public List<Product> addProducts(@RequestBody List<Product> products) {
return service.saveProducts(products);
}
@GetMapping("/products")
public List<Product> findAllProducts() {
return service.getProducts();
}
@GetMapping("/productById/{id}")
public Product findProductById(@PathVariable int id) {
return service.getProductById(id);
}
@GetMapping("/product/{name}")
public Product findProductByName(@PathVariable String name) {
return service.getProductByName(name);
}
@PutMapping("/update")
public Product updateProduct(@RequestBody Product product) {
return service.updateProduct(product);
}
@DeleteMapping("/delete/{id}")
public String deleteProduct(@PathVariable int id) {
return service.deleteProduct(id);
}
}
SpringBootCrudExampleApplication.java
package com.javanovice.crud.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class SpringBootCrudExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCrudExampleApplication.class, args);
}
}
【问题讨论】:
标签: java spring-boot hibernate jpa repository