使用 Spring 进行自注入 [英] Self injection with Spring
问题描述
我在 Spring 3.x 中尝试了以下代码,但失败了 BeanNotFoundException
并且它应该根据我之前问过的一个问题的答案 - 我可以使用 Spring 注入同一个类吗?
I tried the following code with Spring 3.x which failed with BeanNotFoundException
and it should according to the answers of a question which I asked before - Can I inject same class using Spring?
@Service
public class UserService implements Service{
@Autowired
private Service self;
}
自从我用 Java 6 尝试这个之后,我发现以下代码工作正常:
Since I was trying this with Java 6, I found the following code works fine:
@Service(value = "someService")
public class UserService implements Service{
@Resource(name = "someService")
private Service self;
}
但我不明白它是如何解决循环依赖的.
but I don't understand how it resolves the cyclic dependency.
这是错误消息.OP 在对其中一个答案的评论中提到了这一点:
Here's the error message. The OP mentioned it in a comment on one of the answers:
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为 [com.spring.service.Service] 的匹配 bean 依赖项:预期至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.spring.service.Service] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
推荐答案
更新:2016 年 2 月
Self autowiring 将在 Spring Framework 4.3 中得到正式支持.实现可以在这个 GitHub 提交中看到.
Self autowiring will be officially supported in Spring Framework 4.3. The implementation can be seen in this GitHub commit.
您不能自己自动装配的明确原因是 Spring 的 DefaultListableBeanFactory.findAutowireCandidates(String, Class, DependencyDescriptor)
方法的实现明确排除了这种可能性.这在此方法的以下代码摘录中可见:
The definitive reason that you cannot autowire yourself is that the implementation of Spring's DefaultListableBeanFactory.findAutowireCandidates(String, Class, DependencyDescriptor)
method explicitly excludes the possibility. This is visible in the following code excerpt from this method:
for (String candidateName : candidateNames) {
if (!candidateName.equals(beanName) && isAutowireCandidate(candidateName, descriptor)) {
result.put(candidateName, getBean(candidateName));
}
}
仅供参考:bean 的名称(即尝试自动装配自己的 bean)是 beanName
.该 bean 实际上是自动装配候选者,但上述 if 条件返回 false(因为 candidateName
实际上等于 beanName
).因此,您根本无法将 bean 与自身自动装配在一起(至少从 Spring 3.1 M1 开始不是这样).
FYI: the name of the bean (i.e., the bean that's trying to autowire itself) is beanName
. That bean is in fact an autowire candidate, but the above if-condition returns false (since candidateName
in fact equals the beanName
). Thus you simply cannot autowire a bean with itself (at least not as of Spring 3.1 M1).
至于这是否是语义上的预期行为,这是另一个问题.;)
Now as for whether or not this is intended behavior semantically speaking, that's another question. ;)
我会问 Juergen,看看他有什么要说的.
I'll ask Juergen and see what he has to say.
问候,
Sam(核心 Spring 提交者)
Sam (Core Spring Committer)
附言我已经打开了一个 Spring JIRA 问题,以考虑使用 @Autowired 按类型支持自动装配.请随时在此处观看或投票支持此问题:https://jira.springsource.org/browse/SPR-8450
p.s. I've opened a Spring JIRA issue to consider supporting self-autowiring by type using @Autowired. Feel free to watch or vote for this issue here: https://jira.springsource.org/browse/SPR-8450
这篇关于使用 Spring 进行自注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!