3

I have the following main class:

public class Startup implements UncaughtExceptionHandler {

@Autowired
private MessageListener messageListener;

private static Startup startup;

public static void main(String[] args) {
        Startup start = new Startup();
        start.init(args); 
}

public void init(String[] args) {

    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    startup = (Startup) context.getBean( "startup" );
    startup.start(); //here the messageListener is used
}

// here goes the main class that calls the method where messageListener is used }

@Component
public class ProdMessageListener 
    extends AbstractMessageListener implements MessageListener {...}

and

public abstract class AbstractMessageListener 
    implements MessageListener {...}

as well as

@Component
public interface MessageListener extends QueueAware {...}

@Component
public interface QueueAware {...}

My Spring context uses to locate all the classes and interfaces. However the bean is not recognized and I get:

No qualifying bean of type [com.ware.messaging.listener.MessageListener] found for dependency.

Any ideas why autowiring does not work?

luksmir
  • 3,104
  • 5
  • 22
  • 38

2 Answers2

2

Just make sure you have added your base package to the spring context configuration like below to allow spring to load all the components into the container

 <context:component-scan base-package="pakage1.package2"/>
Lavanya
  • 319
  • 1
  • 6
  • The implementation class suppose to be a Component not the interface – Lavanya Apr 11 '13 at 14:48
  • The implementation class is ProdMessageListener which is a Component. Is that correct/incorrect? – luksmir Apr 11 '13 at 14:54
  • Annotating your implementation with @Component and not annotating your interface is usually the right way to set things up. Spring's autowiring will look for a managed bean of a matching type, and your implementation will match for a field typed to the interface. see the below link - http://stackoverflow.com/questions/10318482/spring-interface-loading – Lavanya Apr 11 '13 at 15:11
0

Do you know what the problem was? Spring does not seem to autowire static fields. Now everything works.

luksmir
  • 3,104
  • 5
  • 22
  • 38