我收到此错误: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;@Repositorypublic interface UserRepository extends JpaRepository<DemoModel,Integer> { void save(HashMap<String, DemoModel> data);}演示服务.javapackage 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;@Componentpublic 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;}
1 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
你应该重新定义你的包结构,所有的包都应该在 com.package.demo 里面,否则你的子包比如控制器或存储库不会被 SpringBoot 扫描或尝试使用它
@SpringBootApplication(scanBasePackages={
"com.example.controller",
"com.example.service"
})
添加回答
举报
0/150
提交
取消