spring的三种注入的区别

阅读 97

2022-11-17


@Resource

按名称注入

@Autowired

按类型注入

@RequiredArgsConstructor和private final TestService testService

按类型注入,不允许服务之间循环依赖
如:
TestServiceImpl
TestServiceImpl2
若:
TestServiceImpl里面写了: private final TestService2 testService2那么,在TestServiceImpl2就不能再写: private final TestService testService

解决方法:

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
或改用@Resource或@Autowired


精彩评论(0)

0 0 举报