luksmir
luksmir

Reputation: 3154

Abstract classes, interfaces and Autowired

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?

Upvotes: 3

Views: 1851

Answers (2)

luksmir
luksmir

Reputation: 3154

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

Upvotes: 0

Lavanya
Lavanya

Reputation: 319

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"/>

Upvotes: 2

Related Questions