Search code examples
javacompilationmicroservices

Bean not found with @SpringBootApplication


I have a problem with microservices in JAVA. I do not understand why my code does not want to compile.

I follow a tutorial, video (in French) to create a simple project to become familiar with microservices.

I create a controller, dao and a model. When I compile the controller to access the 127.0.0.1.1port/Produits it must return me the list of the products that I defined in the code BUT at the compilation it shows me that I once have a hand:

"Error: the method main is not found in the class

"while normally to start the project I do not need to hand because it must just tell me" Ok you can go on the 127.0.0.1/Port "(The port is defined in the application. properties and have not occupied)

Here is the architecture of my project:

enter image description here

Here is the code of my controller that I want to compile:

package com.ecommerce.microcommerce.controller;

import com.ecommerce.microcommerce.dao.ProductDao;
import com.ecommerce.microcommerce.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController {

    @Autowired
    private ProductDao productDao;

    //Produits
    @GetMapping(value = "Produits")
    public List<Product> listeProduits() {
        return productDao.finAll();
    }

    //Produits/{id}
    @GetMapping(value = "Produits/{id}")
    public Product afficherUnProduit(@PathVariable int id) {
        Product product = new Product(1, new String("aspirateur"), 100);
        return product;
    }

}

My files from my DAO:

package com.ecommerce.microcommerce.dao;

import com.ecommerce.microcommerce.model.Product;
import java.util.List;

public interface ProductDao {

    public List<Product> finAll();

    public Product finById(int id);

    public Product save(Product product);

}

package com.ecommerce.microcommerce.dao;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.ecommerce.microcommerce.model.Product;

@Repository 
public class ProductDaoImpl implements ProductDao {

    public static List<Product> products = new ArrayList<>();

    static {
        products.add(new Product(1, new String("Ordinateur portable"), 350));
        products.add(new Product(2, new String("Aspirateur robot"), 500));
        products.add(new Product(3, new String("Table de ping pong"), 750));

    }

    @Override
    public List<Product> finAll() {
        return products;
    }
    @Override
    public Product finById(int id) {
        return null;
    }
    @Override
    public Product save(Product product) {
        return null;
    }
}

My files from my Model:

package com.ecommerce.microcommerce.model;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MicrocommerceApplication {

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

package com.ecommerce.microcommerce.model;

public class Product {

    private int id;
    private String name;
    private int prix;

    public Product(int id, String name, int prix) {
        this.id = id;
        this.name = name;
        this.prix = prix;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrix() {
        return prix;
    }

    public void setPrix(int prix) {
        this.prix = prix;
    }

    @Override
    public String toString() {
        return "Product [id=" + id + ", name=" + name + ", prix=" + prix + "]";
    }

}

I saw that a lot of people had a compilation problem on other post but no answer to my problem

Thanks in advance, the code is long but very simple. I never know if I put too much or not enough. I put everything. Thank you


Solution

  • Since your MicrocommerceApplication (Main Class) class and other Beans such as ProductDaoImpl and ProductController are in different packages, Spring is unable to discover them.

    @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration

    The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes: [...]

    The default @ComponentScan is used which searches for beans in current package only.

    If you want a custom configuration, provide your own @Configuration, @EnableAutoConfiguration, and @ComponentScan, as appropriate.

    Solutions to your Problem:

    1. You can use the hack to move MicrocommerceApplication and all the other Beans inside the same package.

    2. In place of @SpringBootApplication you can use:

      package com.ecommerce.microcommerce.model;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      @Configuration
      @EnableAutoConfiguration
      @ComponentScan({
                       "com.ecommerce.microcommerce.controller",
                       "com.ecommerce.microcommerce.dao"
                       "com.ecommerce.microcommerce.model"})
      public class MicrocommerceApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(MicrocommerceApplication.class, args);
          }
      }