程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决虑在配置中定义类型为“ com.ensat.services.Productservice”的bean?

开发过程中遇到虑在配置中定义类型为“ com.ensat.services.Productservice”的bean的问题如何解决?下面主要结合日常开发的经验,给出你关于虑在配置中定义类型为“ com.ensat.services.Productservice”的bean的解决方法建议,希望对你解决虑在配置中定义类型为“ com.ensat.services.Productservice”的bean有所启发或帮助;

该bean不是由Spring创建的,因为componentScanattribute错过了所在的包ProductserviceImpl

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

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

应替换为:

@SpringBootApplication
@ComponentScan(basePackages = {"Hello","com.ensat.controllers", "com.ensat.services";
})
@EntityScan("com.ensat.entitIEs")
@EnableJpaRepositorIEs("com.ensat.repositorIEs")

它将解决您的问题,但是这种方式克服了Spring和Spring Boot的配置优势。

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

@ComponentScan(basePackages = {"Hello","com.ensat.controllers"})
@EntityScan("com.ensat.entitIEs")

@SpringBootApplication课堂上。

例如,移入Applicationcom.ensat软件包并将所有Bean移入该软件包或其中一个子软件包都将解决您的配置问题并减轻您的配置。

package com.ensat;

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

@SpringBootApplication
public class Application extends SpringBootServleTinitializer {
    ...
}

为什么呢

因为@SpringBootApplication包括已经(以及更多):

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@inherited
@SpringBootConfiguration
@EnableautoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = autoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

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

文档参了这一点。

在这里:

这里讨论了@EnableautoConfiguration 77.3 Use Spring Data Repository point提供的关于实体发现的内容 :

解决方法

我有

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

}

ProductController.java

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

}

Productservice.java

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

}

ProductserviceImpl.java

package com.ensat.services;

import com.ensat.entities.Product;
import com.ensat.repositories.ProductRepository;
import org.springframework.beans.factory.Annotation.Autowired;
import org.springframework.stereotype.service;

/**
 * Product service implement.
 */
@service
public class ProductserviceImpl implements Productservice {

    private ProductRepository productRepository;

    @Autowired
    public void setProductRepository(ProductRepository productRepository) {
        this.productRepository = productRepository;
    }

    @Override
    public Iterable<Product> listAllProducts() {
        return productRepository.findAll();
    }

    @Override
    public Product getProductById(Integer id) {
        return productRepository.findOne(id);
    }

    @Override
    public Product saveProduct(Product product) {
        return productRepository.save(product);
    }

    @Override
    public void deleteProduct(Integer id) {
        productRepository.delete(id);
    }

}

这是我的错误:

***************************
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.

我有完整的日志:https
:
//gist.github.com/donhuvy/b918e20eeeb7cbe3c4be4167d066f7fd

这是我的完整源代码
https://github.com/donhuvy/accounTing/commit/319bf6bc47997ff996308c890eba81a6fa7f1a93

如何解决错误?

大佬总结

以上是大佬教程为你收集整理的考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean全部内容,希望文章能够帮你解决考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。