提问者:小点点

在Spring Boot应用程序中无法识别@Repository


尝试运行Spring Boot应用程序时,我收到以下错误。

***************************应用程序启动失败

描述:

com中的字段存储库。Java新手。积垢。实例服务ProductService需要“com”类型的bean。Java新手。积垢。实例存储库。找不到ProductRepository“”。

注入点具有以下注释:

  • @组织。springframework。豆。工厂注释。自动连线(必需=真)

措施:

考虑定义“com”类型的bean。Java新手。积垢。实例存储库。配置中的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);
}

产品ervice.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);
    }

}

共1个答案

匿名用户

您需要删除排除={DataSourceAutoConfiguration.class},如下所示:

@SpringBootApplication
public class SpringBootCrudExampleApplication {

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

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