当前位置:  开发笔记 > 编程语言 > 正文

考虑在配置中定义'service'类型的bean [Spring boot]

如何解决《考虑在配置中定义'service'类型的bean[Springboot]》经验,为你挑选了2个好方法。

我运行主类时遇到错误.

错误:

Action:
Consider defining a bean of type 'seconds47.service.TopicService' in your configuration.

Description:
Field topicService in seconds47.restAPI.topics required a bean of type 'seconds47.service.TopicService' that could not be found

TopicService接口:

public interface TopicService {

    TopicBean findById(long id);

    TopicBean findByName(String name);

    void saveTopic(TopicBean topicBean);

    void updateTopic(TopicBean topicBean);

    void deleteTopicById(long id);

    List findAllTopics(); 

    void deleteAllTopics();

    public boolean isTopicExist(TopicBean topicBean);
}

控制器:

@RestController
public class topics {

    @Autowired
    private TopicService topicService;

    @RequestMapping(path = "/new_topic2", method = RequestMethod.GET)
    public void new_topic() throws Exception {
        System.out.println("new topic JAVA2");
    }
}

实施班:

public class TopicServiceImplementation implements TopicService {

    @Autowired
    private TopicService topicService;

    @Autowired
    private TopicRepository topicRepository;

    @Override
    public TopicBean findById(long id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public TopicBean findByName(String name) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void saveTopic(TopicBean topicBean) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void updateTopic(TopicBean topicBean) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deleteTopicById(long id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List findAllTopics() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deleteAllTopics() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean isTopicExist(TopicBean topicBean) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

其余的类也定义了.尽管componentScan在主要班级宣布,但我不知道为什么它会抛出.

主要课程:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@ComponentScan(basePackages = {"seconds47"})
@EnableJpaRepositories("seconds47.repository")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的包裹是这样的:

seconds47
seconds47.beans
seconds47.config
seconds47.repository
seconds47.restAPI
seconds47.service

dunni.. 38

一个类必须具有@Component注解或该推导(如@Service,@Repository等)被识别为组件扫描一个Spring bean.因此,如果您添加@Component到课程中,它应该可以解决您的问题.



1> dunni..:

一个类必须具有@Component注解或该推导(如@Service,@Repository等)被识别为组件扫描一个Spring bean.因此,如果您添加@Component到课程中,它应该可以解决您的问题.


即使我使用@Component注释了该类,我也会收到该错误

2> Ahmed Tawila..:

既然TopicService是一个Service类,那么您应该用对其进行注释@Service,以便Spring为您自动装配该bean。像这样:

@Service
public class TopicServiceImplementation implements TopicService {
    ...
}

这样可以解决您的问题。

推荐阅读
个性2402852463
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有