0

I am trying to implement as of enterprise level, there they have folders like Repository,Service,ServiceImpl

  1. In Services they have interface with method declaration

  2. In ServiceImpl they have class implementing the interface of services

  3. In Repository they have all Repository interfaces

  4. BeanInjection is a class where we have all repositories and service classes and interfaces with @Autowired annotation.

  5. When I tried to implement "@Autowired" to service class getting this Error.

  6. Tried this no help link

  7. Tried this no help but getting loop error link

Controller.java

public class SessionController extends BeanInjectionService {

    @GetMapping
    public ResponseEntity<List<Session>> list(){
        LOGGER.info("Request received to view the sessions");
        List<Session> sessions = sessionService.findAll();
        LOGGER.info("Successfully fetched all the sessions");
        return new ResponseEntity<>(sessions, HttpStatus.OK);
    }

SessionService.java(Interface)

public interface SessionService {

    List<Session> findAll();
}


SessionServiceImpl.java(Class)

public class SessionServiceImpl  extends BeanInjectionService implements SessionService {

    @Override
    public List<Session> findAll(){
        return sessionRepository.findAll();
    }

BeanInjectionService.java(Class)


public class BeanInjectionService {

    @Autowired
    public SessionRepository sessionRepository;

    **// Error Showing here while starting application
    // Consider defining a bean of type 'com.example.conferencedemo.services.SessionService' in your configuration.**
    @Autowired
    public SessionService sessionService;

    @Autowired
    public SpeakerRepository speakerRepository;

    @Autowired
    public SpeakerService speakerService;

}

SessionRepository.java(Interface)

public interface SessionRepository extends JpaRepository<Session,Long> {

}

Thanks in advance

  • Why do you have a common `BeanInjectionService` interface? `SessionServiceImpl` needs an instance of `SessionService` because the parent defines it so -> somewhat cyclic dependency, where the error might occur – Lino Feb 07 '22 at 08:22
  • Does your `SessionServiceImpl` have a `@Service` annotation? – Abhinav Pandey Feb 07 '22 at 08:25
  • BeanInjectionService is a class where we have all sesionRepository and sessionService variable are defined once and used every where. – Bala Kiran Feb 07 '22 at 08:26
  • @AbhinavPandey tried it getting circular error – Bala Kiran Feb 07 '22 at 08:27
  • When i tried removing @autowired to sessionService and running when accessing the route its saying sessionService is **null** – Bala Kiran Feb 07 '22 at 08:30
  • **ERROR** sessionController (field public com.example.conferencedemo.services.SessionService com.example.conferencedemo.services.BeanInjectionService.sessionService) ┌─────┐ | sessionServiceImpl (field public com.example.conferencedemo.services.SessionService com.example.conferencedemo.services.BeanInjectionService.sessionService) └─────┘ Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. – Bala Kiran Feb 07 '22 at 08:36

1 Answers1

1

I find using BeanInjectionService a little weird, but I'll answer around it.

  1. Unless you add @Service on SessionServiceImpl, you can't autowire it.
  2. Circular dependency - If you do step 1, it will create a circular dependency because SessionServiceImpl needs its superclass object(BeanInjectionService) to be created first. But BeanInjectionService cannot be created unless it finds an object of SessionServiceImpl.
  3. To break the circular dependency, you have only one option. Don't extend BeanInjectionService. Rather, autowire SessionRepository directly into SessionServiceImpl.
@Service
public class SessionServiceImpl  implements SessionService {

    @Autowired
    private SessionRepository sessionRepository;

    @Override
    public List<Session> findAll(){
        return sessionRepository.findAll();
    }
}
Abhinav Pandey
  • 302
  • 2
  • 6