Требуется bean-компонент типа com.example.Resources.UserRepository, который не может быть найден - PullRequest
0 голосов
/ 07 сентября 2018

Я получаю эту ошибку:

Для полевых пользователей в com.example.service.Demoservice требуется компонент типа com.example.Resources.UserRepository, который не найден.

Не могли бы вы помочь

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

Приложили все коды, помогите пожалуйста по этому

1 Ответ

0 голосов
/ 07 сентября 2018

Вам следует переопределить структуру вашего пакета, все пакеты должны быть внутри com.package.demo, иначе SpringBoot не будет сканировать ваши подпакеты, такие как контроллер или репозиторий, или попытаться использовать это

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