https://medium.com/@dulanjayasandaruwan1998/spring-doesnt-recommend-autowiled-autowiled-anymore-05fc053099dadd
基本上可以将其删除,然后将其替换为@Autowired
:
private final
,无论如何,我尝试将其用于repo,它正在工作,但它不起作用。它有这样的错误:
@Service
@RequiredArgsConstructor
public class MyService {
private final MyRepository myRepository;
}
我想做的概念是:
Action:
Despite circular references being allowed, the dependency cycle between beans could not be broken. Update your application to remove the dependency cycle.
在删除@Service
@RequiredArgsConstructor
public class AnotherService {
private final MyService myService;
}
@Service
@RequiredArgsConstructor
public class MyService {
private final AnotherService anotherService;
}
时,我有什么办法可以使它起作用。
您可以使用
@Autowired
注释来懒洋洋地注入豆类,从而避免循环依赖。
建议在构造仪注入中。
添加
@Lazy
或使用spring.main.allow-circular-references=true
@Lazy
ApplicationContext