Consider defining a bean of type 'service' in your configuration [Spring boot]
Solution 1
A class must have the @Component
annotation or a derivation of that (like @Service
, @Repository
etc.) to be recognized as a Spring bean by the component scanning. So if you add @Component
to the class, it should solve your problem.
Solution 2
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.
Solution 3
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);
}
}
Solution 4
I solved this issue by creating a bean for my service in SpringConfig.java file. Please check the below code,
@Configuration
public class SpringConfig {
@Bean
public TransactionService transactionService() {
return new TransactionServiceImpl();
}
}
The path of this file is shown in the below image, Spring boot application folder structure
Solution 5
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.
kittu
Worked on different stack of technologies with 6+ years of experience such as: Front-end stack: Javascript, Angular 7/8, HTML5/CSS3, Bootstrap Back-end stack: NodeJs, MongoDb with mongoose, AWS, RabbitMQ Tools: Git, VS Code, Jenkins Passionate about working on enterprise or product based companies with react, node and mongo tech stack
Updated on July 09, 2022Comments
-
kittu almost 2 years
I get error when I run the main class.
Error:
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 interface:
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); }
controller:
@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"); } }
Implementation 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<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. } }
Rest of the classes are defined too. I don't know why its throwing despite declaring
componentScan
in main class.Main class:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class }) @ComponentScan(basePackages = {"seconds47"}) @EnableJpaRepositories("seconds47.repository") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
I have my packages like this:
seconds47 seconds47.beans seconds47.config seconds47.repository seconds47.restAPI seconds47.service
-
kittu over 7 yearsI didn't understand a thing when you said
I am injecting a bean in itself
?? I am injectingTopicService
which is a interface into a controller classTopics
-
kittu over 7 yearsI didn't edit the error message. I just edited the question title I have posted
-
kittu over 7 yearsStill the problem exists. I removed
@Autowired private TopicService topicService;
from implementation class -
Tushar Banne over 6 yearsI am getting that error even if I have annotated that class with @Component
-
Sumit Rane about 3 years@Service annotation I applied on the interface that's why I was facing the same problem, after applying it on implementation class it worked for me
-
Zhenyria almost 3 years@TusharBanne it will not work if you use annotation for interface of bean, not for bean