Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Autowired doesn't work if component-scan removed

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?

like image 603
GarfieldKlon Avatar asked Nov 01 '12 12:11

GarfieldKlon


1 Answers

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.

like image 154
artbristol Avatar answered Nov 16 '22 01:11

artbristol