@SpringBootApplication未找到Bean

时间:2018-11-06 10:48:05

标签: java compilation microservices

我对JAVA中的微服务有疑问。我不明白为什么我的代码不想编译。

我遵循一个视频教程(法语)来创建一个简单的项目,以熟悉微服务。

我创建一个控制器,dao和一个模型。当我编译控制器以访问127.0.0.1.1port / Produits时,它必须返回我在编译时在代码BUT中定义的产品列表,这表明我曾经有过帮助:

  

“错误:在类中找不到方法main

“在通常情况下启动项目时,我不需要处理,因为它只需要告诉我”“好,您可以继续使用127.0.0.1/Port”(该端口在应用程序中定义。该属性尚未占用)

这是我的项目的体系结构:

enter image description here

这是我要编译的控制器代码:

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;
    }

}

我的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;
    }
}

我的模型文件:

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 + "]";
    }

}

我看到很多人在其他帖子上都有编译问题,但是我的问题没有答案

提前,代码虽然很长,但是非常简单。我永远不知道我投入过多还是不足。我把一切都放了。谢谢

3 个答案:

答案 0 :(得分:4)

由于您的MicrocommerceApplication(主类)类和其他 Bean (例如ProductDaoImplProductController)位于不同的程序包中,因此Spring无法发现它们。

  

@SpringBootApplication = @配置+ @ComponentScan + @EnableAutoConfiguration

@SpringBootApplication注释等效于使用@Configuration@EnableAutoConfiguration@ComponentScan及其默认属性:[...]

使用默认的@ComponentScan,它仅搜索当前程序包中的bean

如果需要自定义配置,请根据需要提供自己的@Configuration@EnableAutoConfiguration@ComponentScan

问题的解决方案:

  1. 您可以使用黑客将MicrocommerceApplication和所有其他Bean 移到同一软件包中

  2. 可以代替@SpringBootApplication使用:

    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);
        }
    }
    

答案 1 :(得分:2)

尝试将MicrocommerceApplication类移至包

  

com.ecommerce.microcommerce

另一件事,默认地址是您的本地主机(127.0.0.1)。

答案 2 :(得分:0)

如果要将主类放在另一个包中,也可以实现自定义运行程序来运行应用程序,例如:

@Component
public class ApplicationRunner implements CommandLineRunner {

        @Autowired
        private ProductController productController;

        @Override
        public void run() {
            //TODO
        }
    }

我希望它能起作用!

相关问题