为什么我需要自动接线/注入场的二传手? [英] Why do I need a setter for autowired / injected field?

查看:110
本文介绍了为什么我需要自动接线/注入场的二传手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个豆子:

    <bean id="BasketLogic" class="efco.logic.EfcoBasketLogic" autowire="byType">
        <property name="documentLogic" ref="DocumentLogic" />
        <property name="stateAccess" ref="StateAccess" />
        <property name="contextAccess" ref="ContextAccess" />
    </bean>

  <bean id="EfcoErpService" autowire="byType" class="efco.erp.service.EfcoErpServiceImpl">
    <constructor-arg ref="ErpConnector"/>
  </bean>

documentLogic stateAccess contextAccess BasketLogicImpl

我没有<context:component-scan />

EfcoBasketLogic.java:

EfcoBasketLogic.java:

public class EfcoBasketLogic extends BasketLogicImpl {

        @Inject
        private EfcoErpService erpService;
    ...
    ...
    ...
}

除非我提供设置器,否则

erpService为 null .但为什么?我以为在进行自动装配的地方不需要二传手?可能是BasketLogicImpl负责吗?

erpService is null, unless I provide a setter. But why? I thought a setter isn't needed where autowiring is taking place? Could it be that BasketLogicImpl is responsible for that?

推荐答案

您需要使用setter,因为除非通过<context:component-scan /><context:annotation-config />告知spring,否则不会检测到注释.由于您指定了autowire="byType",因此检测到Setter.

You need to use a setter because annotations are not detected unless spring is told so through either <context:component-scan /> or <context:annotation-config />. Setter is detected because you specified autowire="byType".

您可能还会发现此问题和答案也很有帮助:何时在春季使用自动装配

You may find this question and answer helpful as well: When to use autowiring in Spring

这篇关于为什么我需要自动接线/注入场的二传手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆