Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 932
kittu Avatar asked Jan 15 '17 16:01

kittu


People also ask

How Bean is defined in configuration Spring boot?

Declaring a bean. To declare a bean, simply annotate a method with the @Bean annotation. When JavaConfig encounters such a method, it will execute that method and register the return value as a bean within a BeanFactory .

What is @bean in Spring boot?

Spring @Bean annotation tells that a method produces a bean to be managed by the Spring container. It is a method-level annotation. During Java configuration ( @Configuration ), the method is executed and its return value is registered as a bean within a BeanFactory .

Does Spring allow more than one bean definition in the configuration?

Spring Couldn't autowired,there is more than one bean of `` type. Bookmark this question.

What is scanBasePackages?

The parameter scanBasePackages does configure the packages to be scanned. If you want to add some extra packages like you did into your example, you have to use this annotation.


2 Answers

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.

like image 83
dunni Avatar answered Sep 21 '22 14:09

dunni


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.

like image 25
Ahmed Tawila Avatar answered Sep 21 '22 14:09

Ahmed Tawila