为什么我需要一个用于自动装配/注入字段的setter?

Gar*_*lon 10 setter spring inject autowired

我有一个豆子:

    <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>
Run Code Online (Sandbox Code Playgroud)

documentLogic,stateAccesscontextAccess上田BasketLogicImpl

我没有 <context:component-scan />

EfcoBasketLogic.java:

public class EfcoBasketLogic extends BasketLogicImpl {

        @Inject
        private EfcoErpService erpService;
    ...
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

erpService为null,除非我提供setter.但为什么?我认为在自动装配发生的地方不需要安装器?可能是BasketLogicImpl对此负责吗?

mre*_*isz 11

你需要,因为没有检测到的注释使用的setter,除非弹簧或者通过这么说<context:component-scan /><context:annotation-config />.由于您指定了,因此检测到Setter autowire="byType".

您可能会发现此问题和答案也很有用:何时在Spring中使用自动装配