需要一个无法找到的“com.example.Resources.UserRepository”

我收到此错误:


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


}

演示服务.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;


}


月关宝盒
浏览 196回答 1
1回答

HUX布斯

你应该重新定义你的包结构,所有的包都应该在 com.package.demo 里面,否则你的子包比如控制器或存储库不会被 SpringBoot 扫描或尝试使用它@SpringBootApplication(scanBasePackages={&nbsp; &nbsp; "com.example.controller",&nbsp;&nbsp; &nbsp; "com.example.service"})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java