当前位置: 首页 > 知识库问答 >
问题:

如何解决这个“需要一个bean”错误

安经纶
2023-03-14

我是Springboot的新手,我通过Sping的教程和“https://spring.io/guides”指南来学习。

现在我试图理解并重做教程:“使用MySQL访问数据”

不幸的是,我在Mavenbuild期间出错:

启动ApplicationContext时出错。要显示自动配置报告,请在启用“调试”的情况下重新运行应用程序。2017-12-07 10:26:42.708错误8100---[main]o.s.b.d.LoggingFailureAnalysisReporter:

应用程序启动失败

说明:

xxx中的字段goodRepository。控制器。GoodController需要类型为“xxx”的bean。存储库。找不到“GoodRepository”。

行动:

考虑定义“xxx”类型的bean。存储库。在您的配置中。

进程结束,退出代码1

这是我的代码:

Good.java:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Data;

@Entity
@Data
public class Good {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String name;

    public Good() {
    }

    public void setName(String name) {
        this.name = name;
    }
}

GoodRepository:

import org.springframework.data.repository.CrudRepository;

import xxx.model.Good;

public interface GoodRepository extends CrudRepository<Good, Long>
{

}

GoodController:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xxx.model.Good;
import xxx.repositories.GoodRepository;

@Controller
@RequestMapping(path="/goods")
public class GoodController {

    @Autowired
    private GoodRepository goodRepository;

    @GetMapping(path="/add")
    public @ResponseBody String addNewGod( @RequestParam String name)
    {
        Good god= new Good();
        god.setName(name);
        goodRepository.save(god);
        return "Saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<Good> getAllGods()
    {
        return goodRepository.findAll();
    }

}

我的申请。属性:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/gccontest
spring.datasource.username=xxx
spring.datasource.password=xxx

此端口上存在mysql db。。。

我哪里做错了什么?

共有3个答案

子车飞鹏
2023-03-14

我已经找到了!!!;-)

我不得不编辑我的申请。java:

package xxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,WebMvcAutoConfiguration.class })
@ComponentScan({ "xxx.repositories"})
public class Application 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(Application.class, args);
    }
}

我还没有检查我的功能,但它正在编译并运行!非常感谢您的提示!!!

岳嘉石
2023-03-14

将您的GoodRepository实现类标记为@service/@组件,然后试一试

燕凯旋
2023-03-14

你需要用@Repository注释你的GoodRepository。答案来自:Spring Boot Inject Crudepository to Service

 @Repository
 public interface GoodRepository extends CrudRepository<Good, Long>{

 }
 类似资料:
  • 我想在一个controller方法中创建一个新的Role对象,然后该对象显示一个链接到Hibernate/H2数据库的数据存储库中的所有“角色”,但每次我尝试创建一个新对象时,都会出现一个SQL错误,对我来说这似乎不对。如果有人能帮忙,那就太好了。 下面是repo-https://github.com/danielturato/instateam-th 对于角色实体,我尝试了以下操作: 将名称字段

  • 配置根项目“我的应用程序”时出现问题。 未能解析配置“:classpath”的所有工件。无法解析com.android.tools.build:gradle:3.6.2。project:脱机模式下没有com.android.tools.build:gradle:3.6.2的缓存版本。脱机模式下没有com.android.tools.build:gradle:3.6.2的缓存版本。 这是我的错误,我

  • 由于我在接口中插入了crudrepository,所以出现了这个错误: 这是我的实体 我当然把getters和setters 和我的控制器: 我在openclassrooms上学习了springboot的课程,我也是这样应用的,只是我在代码中引入了Crudrepository

  • 输入格式 每一行输入都将包含一个字符串,后跟一个整数。每个字符串将有最多的字母字符,每个整数将在从到的包含范围内。 输出格式 在每行输出中,应该有两列:第一列包含字符串,并使用完全字符左对齐。第二列包含整数,以数字表示;如果原始输入少于三位数,则必须用零填充输出的前导数字。 样本输入 样本输出 我试过使用制表符空间。但我不能把这个对齐。 我的输出 预期产出

  • 在我的MergeSort程序的这一部分中,我递归地划分一个名为“arr”的未排序数组。为此,我创建了两个子数组,“leftArr”和“rightArr”,然后我分别用“arr”的前半部分和“arr”的后半部分填充“leftArr”和和“right arr”。然后,我将使用递归来划分/排序leftArr和rightArr。 只是想澄清一下:中=长度; 要初始化rightArr,我执行以下操作: 当我

  • 给定一个整数数组和一个整数目标,返回两个数字的索引,使它们相加为目标。 警告:命令行选项已启用安全管理器警告:安全管理器已弃用,将在将来的版本中删除 JAVAlang.ArrayIndexOutOfBoundsException:索引3超出第6行的长度3的界限,解决方案。twoSum 在第54行,驾驶员解决方案。帮手 第87行Driver.main