0
@Service
public class A implements ServiceA {
    @Autowired
    private ServiceB b;
}

@Service
public class B implements ServiceB {
    @Autowired
    private ServiceC c;
}

@Repository
public class C implements ServiceC {
    @Autowired
    private ServiceA a;
}

When start the application, it will throw the Exception like this "org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name.......org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'ServiceC': Bean with name 'ServiceC' has been injected into other beans ...in its raw version as part of a circular reference, but has eventually been wrapped.".

I know we can use @Lazy or other methods to solve the Exception, but, when I only changed class C's annotation @Repository to @Service, the application starts normally without Exceptions. This confused me, why the application don't throw UnsatisfiedDependencyException? Whats the differences between @Service and @Repository?

ps. I have read some materials like this What's the difference between @Component, @Repository & @Service annotations in Spring?

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
Danger
  • 11
  • 2
  • Since they're technically the same thing, the reason why your application starts normally when all are `@Service`s could be due to the annotation processor processing all annotations of the same type at once, allowing for better dependency resolving in this case. – Kayaman Sep 04 '19 at 08:08
  • Well... The above code doesn't even compile. Please review the code you posted and create a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). – cassiomolin Sep 04 '19 at 08:25
  • This bean post processor adds an advisor to any bean that’s annotated with @Repository so that any platform-specific exceptions are caught and then rethrown as one of Spring’s unchecked data access exceptions. Read more: https://javarevisited.blogspot.com/2017/11/difference-between-component-service.html#ixzz5yXm1IaQY – dassum Sep 04 '19 at 08:44
  • Apparently Spring adds a `PersistenceExceptionTranslationPostProcessor` to beans annotated with `@Repository` in contrast to those annotated with `@Service`. Why this results in the exception? No idea. That being said it is a horrible idea to inject a class from the service layer into the persistence layer; so spring kicking your shin for doing so is good ;-) – Christoph Grimmer Sep 04 '19 at 08:57
  • @ChristophGrimmer-Dietrich there's no evidence that it's actually the post processor that causes the exception. You're right about services being injected to repositories though. – Kayaman Sep 04 '19 at 09:10
  • @Kayaman `true` dat. Hence me saying "Why this results in the exception? No idea." By this I meant that I have not even a clue whether this is the cause - it is just the only real difference I know of between the two annotations :-) – Christoph Grimmer Sep 04 '19 at 09:14

0 Answers0