我有以下主类:
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
}
//这里是调用使用 messageListener 的方法的主类
@Component
public class ProdMessageListener
extends AbstractMessageListener implements MessageListener {...}
和
public abstract class AbstractMessageListener
implements MessageListener {...}
还有
@Component
public interface MessageListener extends QueueAware {...}
@Component
public interface QueueAware {...}
我的 Spring 上下文用于定位所有类和接口(interface)。 然而,这个 bean 没有被识别,我得到:
No qualifying bean of type [com.ware.messaging.listener.MessageListener] found for dependency.
知道为什么 Autowiring 不起作用吗?
最佳答案
只需确保您已将基础包添加到如下所示的 spring 上下文配置中,以允许 spring 将所有组件加载到容器中
<context:component-scan base-package="pakage1.package2"/>
关于java - 抽象类、接口(interface)和 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15951286/