【问题标题】:Spring problem - property injected OK but null when I try to use itSpring问题-当我尝试使用它时,属性注入OK但为null
【发布时间】:2010-12-09 07:46:50
【问题描述】:

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

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

非常感谢任何想法!

谢谢!

【问题讨论】:

    标签: java spring


    【解决方案1】:

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

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

    把这个留在这里,以防其他人做出如此愚蠢的事情,如果没有别的,那就是一种自卑,以阻止我再次这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 2022-11-26
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      相关资源
      最近更新 更多