简体   繁体   English

字段需要一个无法找到的类型的 bean

[英]Field required a bean of type that could not be found

I am a spring boot learner so that I have been trying to create some basics Spring boot application.我是一个 Spring Boot 学习者,所以我一直在尝试创建一些基础的 Spring Boot 应用程序。 I got an error while I tried to run my developed application.我在尝试运行我开发的应用程序时遇到错误。

The error I got was [![ https://i.stack.imgur.com/oyQDi.png][1]][1]我得到的错误是 [![ https://i.stack.imgur.com/oyQDi.png][1]][1]

StoreApiAPP.java: StoreApiAPP.java:

package io.ajithan.springbootstarter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"io.ajithan.springbootstarter"})
public class StoreApiAPP {

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

    }

}

ItemDetails.java: [package name : io.ajithan.springbootstarter.model] ItemDetails.java:[包名:io.ajithan.springbootstarter.model]

package io.ajithan.springbootstarter.model;


public class ItemDetails {
    private Integer itemId;
    private String itemName;
    private Double itemPrice;
    private String itemCategory;

    public ItemDetails() {

    }

    public ItemDetails(Integer itemId, String itemName, Double itemPrice, String itemCategory) {
        this.itemId = itemId;
        this.itemName = itemName;
        this.itemPrice = itemPrice;
        this.itemCategory = itemCategory;
    }


    public Integer getItemId() {
        return itemId;
    }
    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public Double getItemPrice() {
        return itemPrice;
    }
    public void setItemPrice(Double itemPrice) {
        this.itemPrice = itemPrice;
    }
    public String getItemCategory() {
        return itemCategory;
    }
    public void setItemCategory(String itemCategory) {
        this.itemCategory = itemCategory;
    }

}

ItemResponse.java:[package name : io.ajithan.springbootstarter.model] ItemResponse.java:[包名:io.ajithan.springbootstarter.model]

package io.ajithan.springbootstarter.model;

public class ItemResponse {
public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

private String message;
}

StoreController.java:[package name : io.ajithan.springbootstarter.controller] StoreController.java:[包名:io.ajithan.springbootstarter.controller]

package io.ajithan.springbootstarter.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.ajithan.springbootstarter.model.ItemDetails;
import io.ajithan.springbootstarter.model.ItemResponse;
import io.ajithan.springbootstarter.service.StoreService;

@RestController
public class StoreController {
    @Autowired
    private StoreService storeService; 

    @RequestMapping("/getAllItems")
    public List<ItemDetails> getItemDetailsList(){
        return storeService.getItemDetails();
    }

    @RequestMapping("/getSingleItem/{idNumber}")
    public ItemDetails getSingleItem(@PathVariable("idNumber") Integer id) {
        return storeService.getSingleItem(id);
    }

    @RequestMapping(method=RequestMethod.POST,value="/addItem")
    public ItemResponse addSingleItem(@RequestBody ItemDetails itemDetails) {
        return storeService.addSingleItem(itemDetails);
    }


}

StoreService.java:[package name : io.ajithan.springbootstarter.service] StoreService.java:[包名:io.ajithan.springbootstarter.service]

package io.ajithan.springbootstarter.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.ajithan.springbootstarter.model.ItemDetails;
import io.ajithan.springbootstarter.model.ItemResponse;

@Service
public class StoreService {

    @Autowired
    private ItemResponse itemResponse;

    private List<ItemDetails> itemDetailsList = new ArrayList<>(Arrays.asList(
            new ItemDetails(1,"5Rs GOODDAY Biscuit",5.00,"SWEET"),
            new ItemDetails(2,"10Rs GOODAY Biscuit,",10.00,"SALTY"),
            new ItemDetails(3,"25Rs GOODAY Biscuit",25.00,"CREAMY")
            ));
    public List<ItemDetails> getItemDetails()
    {
        return itemDetailsList;
    }
    public ItemDetails getSingleItem(Integer id) {
        return itemDetailsList.stream().filter(n->n.getItemId().equals(id)).findFirst().get();
    }

    public ItemResponse addSingleItem(ItemDetails itemDetails) {
        itemDetailsList.add(itemDetails);
        itemResponse.setMessage("Item added successfully");
        return itemResponse;
    }

}

Can anyone give me solution for this problem?谁能给我解决这个问题?

It doesn't make any sense and is actual dangerous in this case, to autowire the ItemResponse .在这种情况下,自动装配ItemResponse没有任何意义并且实际上是危险的。

By default beans in Spring are singletons, so there is now a single instance of the ItemResponse .默认情况下,Spring 中的 bean 是单例的,因此现在只有一个ItemResponse实例。 Now imagine 50 concurrent threads changing the single instance of ItemResponse , what output do you think each thread will have?现在想象 50 个并发线程改变ItemResponse的单个实例,你认为每个线程会有什么输出?

In your case the only proper solution is to remove the autowired field, and simply construct a new ItemResponse inside the addSingleItem method and return that.在你的情况下,唯一正确的解决方法是删除的自动连接领域,简单地构建一个新的ItemResponse内部addSingleItem方法并返回。 That way there is no shared state.这样就没有共享状态。

@Service
public class StoreService {

    private List<ItemDetails> itemDetailsList = new ArrayList<>(Arrays.asList(
            new ItemDetails(1,"5Rs GOODDAY Biscuit",5.00,"SWEET"),
            new ItemDetails(2,"10Rs GOODAY Biscuit,",10.00,"SALTY"),
            new ItemDetails(3,"25Rs GOODAY Biscuit",25.00,"CREAMY")
            ));

    public List<ItemDetails> getItemDetails()
    {
        return itemDetailsList;
    }
    public ItemDetails getSingleItem(Integer id) {
        return itemDetailsList.stream().filter(n->n.getItemId().equals(id)).findFirst().get();
    }

    public ItemResponse addSingleItem(ItemDetails itemDetails) {
        itemDetailsList.add(itemDetails);
        ItemResponse itemResponse = new ItemResponse();
        itemResponse.setMessage("Item added successfully");
        return itemResponse;
    }
}

You must define the ItemResponse bean in your application:您必须在应用程序中定义 ItemResponse bean:

Start by adding the @Component notation in the ItemResponse class as follows:首先在 ItemResponse 类中添加@Component符号,如下所示:

@Component
public class ItemResponse {
    private String message;

    //** Default Constructor 
    public ItemResponse() {
    }

    //** getter and setter
}

And in somewhere in a class configuration you have to tell spring that ItemResponse is a bean that will need to be injected.在类配置的某个地方,你必须告诉 spring ItemResponse 是一个需要注入的 bean。 If you have a class with the @Configuration notation declare your bean otherwise create a configuration class for example in a package named "config":如果您有一个带有@Configuration符号的类,请声明您的 bean,否则在名为“config”的包中创建一个配置类:

@Configuration
public class AppConfig {

    @Bean
    public ItemResponse itemResponse() {
        return new ItemResponse() ;
    }

}

And don't forget to put a default Constructor in your bean.并且不要忘记在您的 bean 中放置一个默认的构造函数。 Good luck !祝你好运 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (…)中的字段memberRepo需要一个找不到类型的bean - Field memberRepo in (…) required a bean of type that could not be found 字段需要一个类型为…的Bean。 - Field required a bean of type … that could not be found 字段......在......需要一个类型的bean......无法找到错误 - Field ... in ... required a bean of type ... that could not be found Error 需要一个找不到类型的 bean - Required a bean of type that could not be found 字段XXX需要找不到类型为XXX的bean - Field XXX required a bean of type XXX that could not be found 在...中的字段存储库需要一个类型为...的bean,无法找到的NoteRepository - Field repository in …NoteController required a bean of type …NoteRepository that could not be found 字段需要一个在通用JPA DAO体系结构中找不到的类型的bean - Field required a bean of type that could not be found on Generic JPA DAO architecture Spring Field需要一个找不到Spring JPA类型的bean - Spring Field required a bean of type that could not be found Spring JPA Spring boot 字段需要一个找不到类型的 bean - Spring boot Field required a bean of type that could not be found 字段存储库需要找不到类型的 Bean - JAVA - Field repositorio required a Bean of Type That Could not be found - JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM