@Service is not working in Implementation Class

92 views Asked by At

Interface:

public interface registration service

Implementation Class:

@Service
public class RegistrationServiceImpl implements RegistrationService{

    @Autowired
    RegistrationService registrationService;}      

Inside Controller Class:

@Autowired
private RegistrationService registrationService;

Hi guys, would like to get a better understanding regarding to service and autowired annotation. Currently, I am placing the Service annotation in the Implementation class, but when I try to use the Autowired annotation in my controller class, I am still getting this error

'sample.RegistrationService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}\"}}"

May I know why and what would be the solution for it?

Directory of Service:

Sample.service.Registration

Directory of Controller:

Sample.controller.registrationController
2

There are 2 answers

5
Priyabrata Nayak On

EDIT for the correct way of implementation:

create a @Configuration class and define the @Bean for this service

@Configuration
 public class AppConfig{
  @Bean(name = "RegistrationService")
  RegistrationService getRegistrationService(){
    return new RegistrationServiceImpl();
  }
}

If you have more than one implementation for the same interface you can create another bean for the same implementation and use @Qualifier("") below @Autowired for defining what you use

2
Vishvajeet w On

Just remove the @Autowired service interface bean from serviceImpl class and annotate serviceImpl class as @Service and in controller class autowire the bean of serviceImpl class as below

@Autowired RegistrationServiceImpl impl;