Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top