Consider defining a bean of type 'service' in your configuration [Spring boot]

前端 未结 13 1366
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 01:35

I get error when I run the main class.

Error:

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


        
相关标签:
13条回答
  • 2020-12-10 01:48

    Since TopicService is a Service class, you should annotate it with @Service, so that Spring autowires this bean for you. Like so:

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

    This will solve your problem.

    0 讨论(0)
  • 2020-12-10 01:49

    remove annotation configuration like service, repository, components

    @Component
    @Service
    
    0 讨论(0)
  • 2020-12-10 01:50

    You are trying to inject a bean in itself. That's obviously not going to work.

    TopicServiceImplementation implements TopicService. That class attempts to autowire (by field!) a `TopicService. So you're essentially asking the context to inject itself.

    It looks like you've edited the content of the error message: Field topicService in seconds47.restAPI.topics is not a class. Please be careful if you need to hide sensitive information as it makes it much harder for others to help you.

    Back on the actual issue, it looks like injecting TopicService in itself is a glitch on your side.

    0 讨论(0)
  • 2020-12-10 01:55

    In case you were wondering where to add @Service annotation, then make sure you have added @Service annotation to the class that implements the interface. That would solve this problem.

    0 讨论(0)
  • 2020-12-10 01:56

    I fixed the problem adding this line @ComponentScan(basePackages = {"com.example.DemoApplication"}) to main class file, just up from the class name

    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);
    	}
    
    }

    0 讨论(0)
  • 2020-12-10 01:58

    @SpringBootApplication @ComponentScan(basePackages = {"io.testapi"})

    In the main class below springbootapplication annotation i have written componentscan and it worked for me.

    0 讨论(0)
提交回复
热议问题