【问题标题】:Unable to recognize @Repository in Spring boot app在 Spring Boot 应用程序中无法识别 @Repository
【发布时间】: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


    【解决方案1】:

    您需要删除exclude = {DataSourceAutoConfiguration.class },如下:

    @SpringBootApplication
    public class SpringBootCrudExampleApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootCrudExampleApplication.class, args);
        }
    }
    

    否则,对于持久性相关的 bean,包括Repositories,自动配置将不会启动。

    【讨论】:

    • 我这样做了,但仍然遇到同样的错误
    • 您确定重新编译了应用程序吗?如果您这样做,您能否在您的问题中添加Product 代码?谢谢!
    • 我修好了。我在 application.properties 文件中使用 hibernate 前缀而不是 spring.datasource 前缀。另外,不需要添加 @Repository 注释。请参考以下链接。
    • 无论如何感谢您的回复。每天都要学习的东西!
    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2015-09-25
    • 2019-02-26
    相关资源
    最近更新 更多