Java 考虑定义一个类型的豆和*x27;com.ensat.services.ProductService';在您的配置中

Java 考虑定义一个类型的豆和*x27;com.ensat.services.ProductService';在您的配置中,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我有 ProductController.java package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfigur

我有

ProductController.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@ComponentScan(basePackages = {"hello","com.ensat.controllers"})
@EntityScan("com.ensat.entities")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}
package com.ensat.controllers;

import com.ensat.entities.Product;
import com.ensat.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Product controller.
 */
@Controller
public class ProductController {

    private ProductService productService;

    @Autowired
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    /**
     * List all products.
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("products", productService.listAllProducts());
        System.out.println("Returning rpoducts:");
        return "products";
    }

    /**
     * View a specific product by its id.
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("product/{id}")
    public String showProduct(@PathVariable Integer id, Model model) {
        model.addAttribute("product", productService.getProductById(id));
        return "productshow";
    }

    // Afficher le formulaire de modification du Product
    @RequestMapping("product/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        model.addAttribute("product", productService.getProductById(id));
        return "productform";
    }

    /**
     * New product.
     *
     * @param model
     * @return
     */
    @RequestMapping("product/new")
    public String newProduct(Model model) {
        model.addAttribute("product", new Product());
        return "productform";
    }

    /**
     * Save product to database.
     *
     * @param product
     * @return
     */
    @RequestMapping(value = "product", method = RequestMethod.POST)
    public String saveProduct(Product product) {
        productService.saveProduct(product);
        return "redirect:/product/" + product.getId();
    }

    /**
     * Delete product by its id.
     *
     * @param id
     * @return
     */
    @RequestMapping("product/delete/{id}")
    public String delete(@PathVariable Integer id) {
        productService.deleteProduct(id);
        return "redirect:/products";
    }

}
package com.ensat.services;

import com.ensat.entities.Product;

public interface ProductService {

    Iterable<Product> listAllProducts();

    Product getProductById(Integer id);

    Product saveProduct(Product product);

    void deleteProduct(Integer id);

}
ProductService.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
@ComponentScan(basePackages = {"hello","com.ensat.controllers"})
@EntityScan("com.ensat.entities")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

}
package com.ensat.controllers;

import com.ensat.entities.Product;
import com.ensat.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Product controller.
 */
@Controller
public class ProductController {

    private ProductService productService;

    @Autowired
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    /**
     * List all products.
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public String list(Model model) {
        model.addAttribute("products", productService.listAllProducts());
        System.out.println("Returning rpoducts:");
        return "products";
    }

    /**
     * View a specific product by its id.
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("product/{id}")
    public String showProduct(@PathVariable Integer id, Model model) {
        model.addAttribute("product", productService.getProductById(id));
        return "productshow";
    }

    // Afficher le formulaire de modification du Product
    @RequestMapping("product/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        model.addAttribute("product", productService.getProductById(id));
        return "productform";
    }

    /**
     * New product.
     *
     * @param model
     * @return
     */
    @RequestMapping("product/new")
    public String newProduct(Model model) {
        model.addAttribute("product", new Product());
        return "productform";
    }

    /**
     * Save product to database.
     *
     * @param product
     * @return
     */
    @RequestMapping(value = "product", method = RequestMethod.POST)
    public String saveProduct(Product product) {
        productService.saveProduct(product);
        return "redirect:/product/" + product.getId();
    }

    /**
     * Delete product by its id.
     *
     * @param id
     * @return
     */
    @RequestMapping("product/delete/{id}")
    public String delete(@PathVariable Integer id) {
        productService.deleteProduct(id);
        return "redirect:/products";
    }

}
package com.ensat.services;

import com.ensat.entities.Product;

public interface ProductService {

    Iterable<Product> listAllProducts();

    Product getProductById(Integer id);

    Product saveProduct(Product product);

    void deleteProduct(Integer id);

}
我有完整的日志:

这是我的全部源代码


如何修复错误?

bean不是由Spring创建的,因为
componentScan
属性错过了
productserviceinpl
所在的包

此外,
@EnableJpaRepositories
丢失。因此,Spring无法连接您的存储库

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setProductService in com.ensat.controllers.ProductController required a bean of type 'com.ensat.services.ProductService' that could not be found.


Action:

Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration.
应替换为:

@SpringBootApplication
@ComponentScan(basePackages = {"hello","com.ensat.controllers"})
@EntityScan("com.ensat.entities")

它将解决您的问题,但这种做法违背了Spring和Spring Boot在配置方面的优势。

如果
应用程序
bean类位于父包中,则所有其他
bean类属于或属于它的一个子包,您不再需要指定这两个注释:

@SpringBootApplication
@ComponentScan(basePackages = {"hello","com.ensat.controllers", "com.ensat.services";
})
@EntityScan("com.ensat.entities")
@EnableJpaRepositories("com.ensat.repositories")
@springbootplication
类中。

例如,在
com.ensat
包中移动
应用程序
,并将所有bean移动到此包或其子包中,将解决您的配置问题并减轻您的配置。

@ComponentScan(basePackages = {"hello","com.ensat.controllers"})
@EntityScan("com.ensat.entities")
为什么?

因为
@springbootplication
已经包含了它们(以及更多):

但是它使用当前类的包作为
basePackage
值来发现bean/实体/存储库等

文档中提到了这一点。

:

许多Spring Boot开发人员总是对其主类进行注释 使用@Configuration、@EnableAutoConfiguration和@ComponentScan。 因为这些注释经常一起使用(特别是在 如果您遵循上面的最佳实践),Spring Boot将提供 方便的@springboot应用程序替代方案

@SpringBootApplication注释相当于使用 @配置、@EnableAutoConfiguration和@ComponentScan及其 默认属性

这里讨论了
@EnableAutoConfiguration
提供的实体发现:

Spring Boot尝试猜测@Repository的位置 定义,基于它找到的@EnableAutoConfiguration。获取 更多控件,请使用@EnableJpaRepositories注释(来自Spring) 数据(JPA)


我正经历着这个错误,我花了整整一个下午才弄明白。我检查了与此问题相关的每个stackoverflow帖子、spring.io文档和github问题,没有一个答案可以解决问题

总之,我尝试过:

  • 重组和重命名我的包和类
  • 为配置定义备用类
  • 删除或重新生成错误的类
  • 尽可能彻底地注释我的课程
  • 完全定义类
  • 创建多个代码实现以@Autowire my repositories
  • 绝望地把头撞在墙上——好吧,我可能有点越界了
  • 考虑到我有两个存储库,@ComponentScan或@EntityScan注释无法识别它们。总之,我不能用它们实例化或执行任何操作

    我的诀窍是什么?如果你已经尝试过了,我建议你做什么

  • 仔细检查pom.xml中的项目依赖项。该错误可能与您的代码无关(因为它不在我的代码上)
  • 如果您的依赖项是正确的,那么使用修改后的pom.xml创建一个新的Maven项目
  • 更新您的Maven依赖项。值得一提的是,我是通过我的IDE实现的,IDE是Eclipse+Spring工具套件(我不认为Spring工具套件在更新方面有多大区别) 4.组织您的软件包,使其遵循最佳实践(有一篇关于此的好文章)
  • 然后,小心地将文件移回。从bean开始,执行程序,检查错误;然后存储、执行、测试;然后是任何启动脚本;然后控制器等慢慢来,确保一切正常

  • 有时调试这些应用程序会非常痛苦。我有我的一份。我希望有帮助

    我在问题中添加了
    productserviceinpl
    ,因为它是缺少的bean。更改后,它仍然不起作用。您的错误是由以下原因引起的:org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到符合依赖项[com.ensat.repositories.ProductRepository]条件的bean:至少需要1个符合autowire候选项条件的bean。依赖项批注:{}我更新了答案以处理丢失的存储库。试试看:)