Spring问题 - 注入属性OK但是当我尝试使用它时为null

时间:2010-12-08 23:27:55

标签: java spring

我有一个类,它会将两个属性注入其中:

private CustomRatesService customRatesService;
private TokenAuthenticator tokenAuthenticator;

public void setCustomRatesService(CustomRatesService customRatesService) {
    this.customRatesService = customRatesService;
}

public void setTokenAuthenticator(TokenAuthenticator tokenAuthenticator) {
    this.tokenAuthenticator = tokenAuthenticator;
}

这是applicationContext.xml:

<bean id="customRatesServiceBean" class="com.ms.rate.service.SetCustomRatesResourceImpl">
<property name="tokenAuthenticator" ref="tokenAuthenticator"/>    
  <property name="customRatesService" ref="customRatesService"/>
</bean>

包含bean的services.xml文件

<bean id="customRatesService" class="com.ms.rate.service.CustomRatesServiceImpl">
  <property name="customRateDao" ref="customRateDao" />
</bean>

<bean id="tokenAuthenticator" class="com.ms.rate.service.TokenAuthenticatorImpl">
  <property name="authenticationUrl" value="${AUTHENTICATION_URL}"/>
</bean>

然而,如果我在方法中放置一个断点,我可以看到tokenAuthenticator总是为null,尽管另一个属性有一个值

可能是因为该类有WebService注释吗?我完全不知所措。

我把调试放到了所有地方,我可以看到:

必然会创建tokenAuthenticator bean。 此外,它肯定是注入!如果我从类中的injecting方法打印出引用,我可以看到一个有效的引用。当我去使用它时它只是空的。

非常感谢任何想法!!

谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,结果证明这是个愚蠢的错误。

出于某种原因,我曾使用该类作为两个bean的实现。调试器向我展示了调用器被调用,但是在创建我没有使用的bean时。在我使用的bean上,我没有添加第二个属性。尔加...

把这个留在这里以防万一其他人做了一些如此愚蠢的事情,如果没有其他事情,那么作为一点点自我羞辱来阻止我再次这样做。