简体   繁体   中英

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.

Now we are trying to update to Spring Boot 2.6 which prohibits circular references by default.

Is there a better way to call local methods and get the @Transactional and other annotations respected than the @Lazy self reference?

you can use @Resource annotation for injection self

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). 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). Consider factoring out the affected methods to a separate delegate bean in such a scenario. Alternatively, you can use @Resource, which may obtain a proxy back to the current bean by its unique name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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