字段 topicRepository 需要一个无法找到的“hello_package.topic_controller.TopicRepository”类型的 bean

时间:2021-05-06 18:13:24

标签: java spring spring-boot spring-mvc post

hello_package.topic_controller.HelloTopicController 中的字段 topicRepository 需要一个 bean 无法找到“hello_package.topic_controller.TopicRepository”类型。

添加了@Service 但它不起作用。 解决了所有依赖项但不起作用和项目结构

这是我的 main_class,

package hello_package;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class HelloSpring {
    public static void main(String []args ){

        SpringApplication.run( HelloSpring.class,args);
    }
}

这是控制器

package hello_package.topic_controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
public class HelloTopicController {

    @Autowired
    TopicRepository topicRepository;

    public List<Topic> arr = new ArrayList<> (Arrays.asList(
            new Topic (1,"java","bad language"),
            new Topic (2,"cpp","good language"),
            new Topic (3,"python","good language")
    ));

    @GetMapping("/topics")
    public List<Topic> getAllTopics(){
        return arr;
    }


    @PostMapping("/add")
    public String addTopic(@RequestBody Topic topic){
        arr.add (topic);
        return "Successfully Added";
    }

    @PostMapping("/add2")
    public String addTopicMethod2(@RequestParam int id,
                                  @RequestParam String name ,
                                  @RequestParam String description ){
        Topic topic = new Topic (id,name,description);
        topicRepository.save (topic);
        return "Successfully Added";
    }

    @PutMapping("/update/{id}")
    public String updateTopic( @RequestBody Topic topic, @PathVariable("id") int id ){

        for( int i=0; i< arr.size (); i++ ){
            if( arr.get (i).getId () == id ){
                arr.get (i).setTopic( topic );
                return "Successfully Updated ";
            }
        }
        return "Topic with this ID not found ";
    }

}

这是服务类

package hello_package.topic_controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class ServiceClass {

    @Autowired
    private TopicRepository topicRepository;
    public List<Topic> listAllUser() {
        return (List<Topic>) topicRepository.findAll();
    }

    public void saveUser(Topic topic) {
        topicRepository.save(topic);
    }

    public Topic getUser(Integer id) {
        return topicRepository.findById(id).get();
    }

    public void deleteUser(Integer id) {
        topicRepository.deleteById(id);
    }
}

0 个答案:

没有答案