【问题标题】:Required a bean of type 'com.example.Resources.UserRepository' that could not be found需要一个找不到的“com.example.Resources.UserRepository”类型的 bean
【发布时间】:2018-09-07 10:33:30
【问题描述】:

我收到此错误:

com.example.service.Demoservice 中的字段 urs 需要一个无法找到的“com.example.Resources.UserRepository”类型的 bean。

请帮忙

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.example.Resources.UserRepository;
@SpringBootApplication

@ComponentScan("com.example")
@EnableJpaRepositories("com.example")

public class DemoApplication {
    public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
      System.out.println("Started");
    }
}

UserRepository.java 包 com.example.Resources;

import java.util.HashMap;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.example.model.DemoModel;
@Repository

public interface UserRepository extends JpaRepository<DemoModel,Integer> {

    void save(HashMap<String, DemoModel> data);

}

DemoService.java

package com.example.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.example.Resources.UserRepository;
import com.example.model.DemoModel;
@Component
public class Demoservice
{@Qualifier("UserRepository")
    @Autowired

    UserRepository urs;
HashMap<String,DemoModel> hash=new HashMap<>();
{
DemoModel dm=new DemoModel();
dm.setId(20);
dm.setName("Priya");
dm.setDept("cse");
hash.put("1", dm);
DemoModel demo=new DemoModel();
demo.setId(26);
demo.setName("Vijaya");
demo.setDept("IT");
hash.put("2", demo);
}

public HashMap<String, DemoModel> getAll()
{
    return hash;

}
public DemoModel getbyid(String id)
{   if(hash.containsKey(id))
{
    return hash.get(id);
}
return null;
}
public void post(HashMap<String, DemoModel> hashmap) 
{
    if(hashmap.keySet()!=null)
    {

     hash.putAll(hashmap);
}
    }
public List<DemoModel> find()
{
    List<DemoModel> list=new ArrayList<DemoModel>();
    list=urs.findAll();
    return list;
}
}

DemoController.java

package com.example.controller;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

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.PostMapping;
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 com.example.Resources.UserRepository;
import com.example.model.DemoModel;
import com.example.service.Demoservice;

@RestController
public class DemoControler 
{
    @Autowired
    Demoservice dms;
    @RequestMapping("/welcome")
    public HashMap<String, DemoModel> getAll()
    {
        return dms.getAll();

    }
    @GetMapping("{id}")

    public DemoModel getbyid(@PathVariable("id") String id)
    {
        return dms.getbyid(id);
    }

    @RequestMapping(value="/setmap",method=RequestMethod.POST)
    public void Postmap(@RequestBody HashMap<String,DemoModel> hashmap)
    {
         dms.post(hashmap);

    }
    /*@RequestMapping(value="/save",method=RequestMethod.POST)
    public void save(@RequestBody HashMap<String,DemoModel> data) {
        dms.save(data);
    }*/
    @GetMapping("/retrive")
    public List<DemoModel> find()
    {
        return dms.find();
    }
}

已附上所有代码,请帮忙

【问题讨论】:

  • 你能发帖Demoservice 吗?

标签: java spring-boot-admin


【解决方案1】:

你应该重新定义你的包结构,所有的包都应该在 com.package.demo 中,否则你的子包比如控制器或存储库将不会被 SpringBoot 扫描或尝试使用这个

@SpringBootApplication(scanBasePackages={
    "com.example.controller", 
    "com.example.service"
})

【讨论】:

  • 我已经添加了下面的代码,所以我认为它会同时扫描 .@ComponentScan("com.example") @EnableJpaRepositories("com.example"),请确认
  • 非常感谢,它正在工作:)
猜你喜欢
  • 1970-01-01
  • 2018-02-05
  • 2017-09-17
  • 2020-07-06
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
相关资源
最近更新 更多