考虑在配置中定义一个’service’类型的bean
我运行主类时遇到错误。
错误:
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"); } }
实施class:
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
一个类必须具有@Component
注释或其派生(如@Service
@Repository
, @Repository
等)才能被组件扫描识别为Spring bean。 因此,如果将@Component
添加到类中,它应该可以解决您的问题。
由于TopicService
是一个Service
类,因此您应该使用@Service
对其进行批注,以便Spring为您自动assembly此bean。 像这样:
@Service public class TopicServiceImplementation implements TopicService { ... }
这将解决您的问题。
你正在尝试注入一个bean本身。 这显然不会起作用。
TopicServiceImplementation
实现了TopicService
。 该类试图(通过字段!)自动assembly一个`TopicService。 所以你基本上要求上下文注入自己。
看起来您已经编辑了错误消息的内容: Field topicService in seconds47.restAPI.topics
不是类。 如果您需要隐藏敏感信息,请小心,因为这会让其他人更难以帮助您。
回到实际问题,看起来注入TopicService
本身就是一个小故障。
我通过替换损坏的jar文件解决了。
但要找到那些损坏的jar文件,我必须在三个IDE中运行我的应用程序 – 1)Intellij Idea 2)NetBeans 3)Eclipse。
Netbeans给了我最大数量的损坏jar的信息。 在Netbeans以及运行中,我使用构建选项(在右键单击项目之后)了解有关损坏的jar的更多信息。
我花了15个多小时才找出这些错误的根本原因。 希望它能帮到任何人。
你必须更新你的
scanBasePackages = { "com.exm.java" }
添加服务路径(用@service注释后)