繁体   English   中英

春季问题-属性注入确定,但是当我尝试使用它时为null

[英]Spring problem - property injected OK but null when I try to use it

我有一个将两个属性注入其中的类:

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肯定是被创建的。 另外,它肯定会被注入! 如果我从类中的注入方法打印出引用,则可以看到有效的引用。 当我去使用它时,它只是null。

非常感谢任何想法!

谢谢!

好的,事实证明这对我来说是一个愚蠢的错误。

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

只是把它留在这里,以防万一其他人做一些愚蠢的事情,如果没有别的,那么有点自卑,使我不敢再做一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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