在@SpringBootApplication中找不到Bean



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

我按照教程,视频(法语(创建了一个简单的项目来熟悉微服务。

我创建了一个控制器、dao和一个模型。当我编译控制器以访问127.0.0.1.1port/Productuits时,它必须向我返回我在代码中定义的产品列表,但在编译时,它向我表明我曾经参与过:

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

"在正常情况下启动项目时,我不需要提交,因为它必须告诉我"好的,你可以继续127.0.0.1/端口"(端口在application.properties中定义,尚未占用(

以下是我项目的架构:

在此处输入图像描述

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

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

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

提前感谢,代码很长,但很简单。我永远不知道我投入的是太多还是不够。我把所有东西都放了。谢谢

由于您的MicrocommerceApplication(Main class(类和其他Beans(如ProductDaoImplProductController(位于不同的包中,Spring无法发现它们。

@SpringBootApplication=@Configuration+@ComponentScan+@EnableAutoConfiguration

@SpringBootApplication注释等效于使用具有默认属性的@Configuration@EnableAutoConfiguration@ComponentScan:[…]

默认的@ComponentScan用于仅搜索当前包中的bean

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

问题解决方案:

  1. 您可以使用破解将MicrocommerceApplication和所有其他Beans移动到同一个包内

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

尝试将MicrocommerceApplication类移动到包装

com.ecommerce.microcommerce

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

如果您希望主类位于另一个包中,您还可以实现自定义运行程序来运行应用程序,例如:

@Component
public class ApplicationRunner implements CommandLineRunner {
@Autowired
private ProductController productController;
@Override
public void run() {
//TODO
}
}

我希望它能起作用!

相关内容

最新更新