考虑在您的配置中定义一个“服务”类型的 bean \[Spring boot\]

新手上路,请多包涵

运行主类时出现错误。

错误:

 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

主题服务接口:

 public interface TopicService {

    TopicBean findById(long id);

    TopicBean findByName(String name);

    void saveTopic(TopicBean topicBean);

    void updateTopic(TopicBean topicBean);

    void deleteTopicById(long id);

    List<TopicBean> 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<TopicBean> 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

原文由 kittu 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 827
2 个回答

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

原文由 dunni 发布,翻译遵循 CC BY-SA 3.0 许可协议

我解决了将这一行添加到主类文件中的问题 @ComponentScan(basePackages = {"com.example.DemoApplication"}) 从类名开始

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.DemoApplication"})
public class DemoApplication {

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

}

原文由 Luis Alfredo Serrano Díaz 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题