@Validated services cannot autowire themselves

前端 未结 1 2001
醉梦人生
醉梦人生 2021-01-23 08:02

If a Service class is annotated with the Validated annotation, then the same class cannot autowire itself.

@Service
@Validated
public class SomeService {
    @Au         


        
相关标签:
1条回答
  • 2021-01-23 08:39

    The annotation @Validated has nothing to do with the incorrect autowiring in this case. Read the error properly and you find out what is wrong. The first line is self-describing:

    Error creating bean with name 'someService': Bean with name 'someService' has been injected into other beans [someService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

    You autowire the very same service into itself which causes the endless circular bean wiring. A solution? Don't design beans like that. If you are really restricted to this design, use either:

    • Setter injection
    • @Lazy or @PostConstruct annotations

    More about this issue at Baeldung's webpage: https://www.baeldung.com/circular-dependencies-in-spring

    0 讨论(0)
提交回复
热议问题