春天无法注入'java.lang.Class
问题描述:
我创建了一个堆类,如下所示,我的目的是初始化堆栈中类型E的元素的大小。春天无法注入'java.lang.Class
应该怎么注入这个类?
但是弹簧抛出如下错误,我该如何解决这个问题?在堆叠构造的
@Service
public class Stack<E> {
int MAX_SIZE = 15;
E[] elements;
@SuppressWarnings("unchecked")
Stack(Class<E> clazz) {
elements = (E[]) Array.newInstance(clazz, MAX_SIZE);
}
volatile int i;
public void push(E e) throws InterruptedException {
while (i > MAX_SIZE) {
Thread.currentThread().wait();
}
elements[i++] = e;
Thread.currentThread().notifyAll();
}
public E pop() throws InterruptedException {
while (i < 0) {
Thread.currentThread().wait();
}
Thread.currentThread().notifyAll();
return elements[i--];
}
}
参数0所需类型的bean的java.lang.Class中“不能被发现。
答
包括0参数的构造
public Stack(){
}
答
你可以使用默认的Java堆栈实现。
https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
+0
我只是试着用spring创建泛型数组。 – Curious
你可以添加你的bean配置文件。 – 2017-03-05 12:27:56
added @Autowired \t java.lang.Class clazz;导致空指针异常。如何通过构造函数注入? –
Curious
你为什么要重新发明轮子?使用LinkedBlockingDeque。顺便说一句,即使你真的想要你自己的实现,它不应该扩展线程,它不应该需要一个类作为参数。只需使用Object []来存储元素。 –