8

我面临的问题是,如果我从配置中删除组件扫描标记,则注释 @Autowired 不再起作用(在所有使用此注释的 Java 类中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="efco.auth" />

here are some beans...

efco.auth 包中只有一个类,这个类与后面的类 EfcoBasketLogic 没有关系。

和一个使用@Autowired 的类:

package efco.logic;
    public class EfcoBasketLogic extends BasketLogicImpl {

        @Autowired
        private EfcoErpService erpService;

这个 Bean 在另一个 spring 配置文件中定义:

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

如您所见,erpService 没有定义。其他三个属性在 BasketLogicImpl 上并具有设置器。

我做错了什么?

4

2 回答 2

15

正如 Tomasz 所说,你需要<context:annotation-config/>工作@Autowired。当您拥有 时<context:component-scan/>,它会为您隐含地包含在内annotation-config

于 2012-11-01T13:07:57.550 回答
0

将其中一个autowire="byType"或添加autowire="byName"到您的 bean 声明中应该可以完成这项工作。

于 2012-11-01T13:07:03.153 回答