I get error when I run the main class.
Error:
Action:
Consider defining a bean of type \'seconds47.service.TopicService\' in your c
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.
remove annotation configuration like service, repository, components
@Component
@Service
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.
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.
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);
}
}
@SpringBootApplication @ComponentScan(basePackages = {"io.testapi"})
In the main class below springbootapplication annotation i have written componentscan and it worked for me.