title: Field required a bean of type ... that could not be found - Error when using Springboot and Postgres
body: <p>we are getting this error when we run our application.
  We have no idea how to fix it, we have tried many solution from other SO questions but none have worked so far.</p>
  <hr />
  <pre><code>**
  APPLICATION FAILED TO START
  **
  
  Description:
  
  Field productRepository in ntnu.no.SpringDatabaseTest.service.ProductService required a bean of type 'ntnu.no.SpringDatabaseTest.repositories.ProductRepository' that could not be found.
  
  
  Action:
  
  Consider defining a bean of type 'ntnu.no.SpringDatabaseTest.repositories.ProductRepository' in your configuration.
  </code></pre>
  <hr />
  <p>We are using PostGres database</p>
  <hr />
  <p>Directory structure:</p>
  <pre><code>src/
  ├── main/
  │   └── java/
  |       ├── ntnu.no.SpringDatabaseTest/
  |       |   └── Bootstrap
  |       |   |   └──BootStrapData
  |       |   └── Controllers
  |       |   |   └──ProductController
  |       |   └── model
  |       |   |   └──Product
  |       |   └── repositories
  |       |   |   └──ProductRepository
  |       |   └── service
  |       |   |   └──IProductService
  |       |   |   └──ProductService
  |       |   └── SpringDatabaseTestApplication
  └── resources/
      └── application.properties
  </code></pre>
  <hr />
  <p>BootStrapData.java</p>
  <pre><code>package ntnu.no.SpringDatabaseTest.Bootstrap;
  
  public class BootStrapData {
  }
  </code></pre>
  <hr />
  <p>ProductController.java</p>
  <pre><code>package ntnu.no.SpringDatabaseTest.Controllers;
  
  import ntnu.no.SpringDatabaseTest.model.Product;
  import ntnu.no.SpringDatabaseTest.service.ProductService;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  
  import java.util.List;
  
  @RestController
  public class ProductController {
  
      private final ProductService productService;
  
      public ProductController(ProductService productService) {
          this.productService = productService;
      }
  
  
      @GetMapping(&quot;/showProducts&quot;)
      public String findProducts(Model model) {
          var products = (List&lt;Product&gt;) productService.findAll();
  
          model.addAttribute(&quot;products&quot;, products);
  
          return &quot;showProducts&quot;;
      }
  
  }
  </code></pre>
  <hr />
  <p>Product.java</p>
  <pre><code>package ntnu.no.SpringDatabaseTest.model;
  
  import org.springframework.stereotype.Service;
  
  import javax.persistence.*;
  
  @Entity
  @Table(name=&quot;products&quot;)
  public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long productID;
      private int price;
      private String size;
      private String productCategory;
      private String productName;
      private int productAmount;
  
      private String productImage;
  
      public Product() {
      }
  
      @Override
      public String toString() {
          return &quot;Product{&quot; +
                  &quot;productID=&quot; + productID +
                  &quot;, price=&quot; + price +
                  &quot;, size='&quot; + size + '\'' +
                  &quot;, productCategory='&quot; + productCategory + '\'' +
                  &quot;, productName='&quot; + productName + '\'' +
                  &quot;, productAmount=&quot; + productAmount +
                  &quot;, productImage='&quot; + productImage + '\'' +
                  '}';
      }
  
      public Long getProductID() {
          return productID;
      }
  
      public void setProductID(Long productID) {
          this.productID = productID;
      }
  
      public int getPrice() {
          return price;
      }
  
      public void setPrice(int price) {
          this.price = price;
      }
  
      public String getSize() {
          return size;
      }
  
      public void setSize(String size) {
          this.size = size;
      }
  
      public String getProductCategory() {
          return productCategory;
      }
  
      public void setProductCategory(String productCategory) {
          this.productCategory = productCategory;
      }
  
      public String getProductName() {
          return productName;
      }
  
      public void setProductName(String productName) {
          this.productName = productName;
      }
  
      public int getProductAmount() {
          return productAmount;
      }
  
      public void setProductAmount(int productAmount) {
          this.productAmount = productAmount;
      }
  
      public String getProductImage() {
          return productImage;
      }
  
      public void setProductImage(String productImage) {
          this.productImage = productImage;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          Product product = (Product) o;
  
          return productID != null ? productID.equals(product.productID) : product.productID == null;
      }
  
      @Override
      public int hashCode() {
          return productID != null ? productID.hashCode() : 0;
      }
  }
  </code></pre>
  <hr />
  <p>Product Interface</p>
  <pre><code>package ntnu.no.SpringDatabaseTest.repositories;
  
  import ntnu.no.SpringDatabaseTest.model.Product;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.stereotype.Repository;
  
  
  @Repository
  public interface ProductRepository extends CrudRepository&lt;Product, Long&gt; {
  }
  </code></pre>
  <hr />
  <p>ProductService Interface</p>
  <pre><code>package ntnu.no.SpringDatabaseTest.service;
  
  
  import ntnu.no.SpringDatabaseTest.model.Product;
  import org.springframework.stereotype.Service;
  
  import java.util.List;
  
  @Service
  public interface IProductService {
  
      List&lt;Product&gt; findAll();
  }
  </code></pre>
  <hr />
  <p>ProductService.java</p>
  <pre><code>package ntnu.no.SpringDatabaseTest.service;
  
  
  import ntnu.no.SpringDatabaseTest.repositories.ProductRepository;
  import ntnu.no.SpringDatabaseTest.model.Product;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import java.util.List;
  
  /**
   * Business logic for products
   */
  @Service
  public class ProductService implements IProductService {
  
      @Autowired
      private ProductRepository productRepository;
  
  
      @Override
      public List&lt;Product&gt; findAll() {
  
          return (List&lt;Product&gt;) productRepository.findAll();
      }
  }
  </code></pre>
  <hr />
  <p>SpringDatabaseTestApplication.java</p>
  <pre><code>package ntnu.no.SpringDatabaseTest;
  
  import ntnu.no.SpringDatabaseTest.repositories.ProductRepository;
  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 SpringDatabaseTestApplication{
  
      public static void main(String[] args) {
          SpringApplication.run(SpringDatabaseTestApplication.class, args);
      }
  
  }
  </code></pre>
  <hr />
  <p>application.properties</p>
  <pre><code>spring.main.banner-mode=off
  logging.level.org.springframework=ERROR
  
  spring.jpa.hibernate.ddl-auto=none
  
  spring.sql.init.mode=always
  spring.sql.init.platform=postgres
  
  
  spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
  </code></pre>
  <hr />
  <p>pom.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.7.0&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;ntnu.no&lt;/groupId&gt;
      &lt;artifactId&gt;SpringDatabaseTest&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;SpringDatabaseTest&lt;/name&gt;
      &lt;description&gt;Test project for Spring Boot&lt;/description&gt;
      &lt;properties&gt;
          &lt;java.version&gt;17&lt;/java.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
  
  
  
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>