8

I'm facing the problem, that the annotation @Autowired doesn't work anymore (in all Java classes that uses this annotation) if I remove the component-scan tag from config

<?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...

There is only one class in the efco.auth package, and this one has no relation to the following class EfcoBasketLogic.

and a class that uses @Autowired:

package efco.logic;
    public class EfcoBasketLogic extends BasketLogicImpl {

        @Autowired
        private EfcoErpService erpService;

This Bean is defined in an other spring config file:

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

As you can see, erpService is not defined. The other three properties are on BasketLogicImpl and have setters.

What I'm doing wrong?

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
GarfieldKlon
  • 11,170
  • 7
  • 31
  • 33
  • 4
    Well, `@Autowired` annotation is picked up only if you use `` or ``. – Tomasz Nurkiewicz Nov 01 '12 at 12:49
  • Ok, it work's if I remove the `` and adding autowire="byType" to the `` AND adding a setter for the field erpService, otherwise it doesn't work. Which is strange, I thought (and read) that a setter is not required if autowiring is used. – GarfieldKlon Nov 02 '12 at 10:15

2 Answers2

15

As Tomasz says, you need <context:annotation-config/> for @Autowired to work. When you had <context:component-scan/>, it implicitly included annotation-config for you.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
artbristol
  • 32,010
  • 5
  • 70
  • 103
0

Adding either autowire="byType" or autowire="byName" to your bean declaration should do the job.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Georg Engel
  • 917
  • 9
  • 16