简体   繁体   English

@Lazy 自引用导致的循环依赖。 Spring 2.6的任何解决方法?

[英]Circular dependencies due to @Lazy self reference. Any workaround for Spring 2.6?

We use circual dependencies with via constructor injection of @Lazy reference to self type so that we get a way to use local methods with @Transactional and other annotations.我们通过构造函数注入@Lazy对 self 类型的引用来使用循环依赖,这样我们就可以使用带有@Transactional和其他注释的本地方法。

Now we are trying to update to Spring Boot 2.6 which prohibits circular references by default.现在我们正在尝试更新到默认情况下禁止循环引用的 Spring Boot 2.6。

Is there a better way to call local methods and get the @Transactional and other annotations respected than the @Lazy self reference?有没有比@Lazy自引用更好的方法来调用本地方法并获得@Transactional和其他注释?

you can use @Resource annotation for injection self您可以使用@Resource注解进行自我注入

class MyClass {
   @Resource
   private MyClass self;
}

paragraph in spring doc 春季文档中的段落

As of 4.3, @Autowired also considers self references for injection (that is, references back to the bean that is currently injected).从 4.3 开始,@Autowired 还考虑注入的自引用(即,引用回当前注入的 bean)。 Note that self injection is a fallback.请注意,自注入是一种后备。 Regular dependencies on other components always have precedence.对其他组件的常规依赖始终具有优先权。 In that sense, self references do not participate in regular candidate selection and are therefore in particular never primary.从这个意义上说,自我参考不参与常规的候选人选择,因此尤其不是主要的。 On the contrary, they always end up as lowest precedence.相反,它们总是以最低优先级结束。 In practice, you should use self references as a last resort only (for example, for calling other methods on the same instance through the bean's transactional proxy).在实践中,您应该仅将自引用用作最后的手段(例如,通过 bean 的事务代理在同一实例上调用其他方法)。 Consider factoring out the affected methods to a separate delegate bean in such a scenario.在这种情况下,考虑将受影响的方法分解为单独的委托 bean。 Alternatively, you can use @Resource, which may obtain a proxy back to the current bean by its unique name.或者,您可以使用@Resource,它可以通过其唯一名称获取返回到当前 bean 的代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM