问题描述
我是新来的春天,我不喜欢使用的.xml作为spring配置。所以我想只用注释。以下是我的测试code,效果很好没有春天,但如果我使用DI和添加注释@资源,我得到NullPointException,任何人都可以用它的帮助,非常感谢。
I'm new to Spring and I don't like to use .xml as the spring configuration. So I want to use only annotation. Following is my test code, it works well without spring, but if I use DI and add annotation "@Resource", I get NullPointException, could anyone help with it, thanks a lot.
我已经进口弹簧相关的罐子。这是一个生产者 - 消费者的情况下,我想注入资源池生产国和消费国。
I have imported related jar of spring already. It's a producer-consumer case, and I want to inject the resource-pool to producer and consumer.
Producer.java
Producer.java
import javax.annotation.Resource;
public class Producer implements Runnable{
@Resource
private ResourcePool qs;
@Override
public void run() {
while (true) {
for(int i = 0;i < Integer.MAX_VALUE; i++) {
try {
qs.produce(i);
System.out.println("Add index = " + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Consumer.java
Consumer.java
import javax.annotation.Resource;
public class Consumer implements Runnable {
@Resource
private ResourcePool qs;
@Override
public void run() {
while(true) {
for (int i = 0; i < 5; i++) {
try {
System.out.println("Remove index = " + qs.comsume());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
ResourcePool.java
ResourcePool.java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ResourcePool {
private BlockingQueue<Integer> aBlockingQueue = new ArrayBlockingQueue<Integer>(11);
public void produce(Integer ins) throws InterruptedException{
aBlockingQueue.put(ins);
}
public int comsume() throws InterruptedException{
return aBlockingQueue.take();
}
}
主要功能
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestPC {
public static void main(String[] args){
ExecutorService aExecutor = Executors.newFixedThreadPool(2);
Producer productor = new Producer();
Consumer consumer = new Consumer();
aExecutor.execute(productor);
aExecutor.execute(consumer);
}
}
异常消息:
在异常线程池1线程2显示java.lang.NullPointerException
在testbq.Consumer.run(Consumer.java:14)在
java.util.concurrent.ThreadPoolExecutor.runWorker(来源不明)在
java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(来源不明)在
螺纹java.lang.Thread.run(来源不明)异常
池1线程1在显示java.lang.NullPointerException
testbq.Producer.run(Producer.java:14)在
java.util.concurrent.ThreadPoolExecutor.runWorker(来源不明)在
java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(来源不明)在
java.lang.Thread.run(来源不明)
感谢
推荐答案
在搜索XML-少Spring配置或的你应该有大概一两分钟内理解了它自己。
On searching "XML-less Spring configuration" or Java-based Spring configuration you should have probably figured it out yourself within a couple of minutes.
您还没有确定这是由被注解为 @Configuration
A类托管任何豆creatiion方法 - 所以春天是能够知道你真正想要什么注入。
You haven't defined any bean-creatiion method which is hosted by a class that is annotated with @Configuration
- so Spring is able to know what you actually want to inject.
@Configuration
// optional @ComponentScan(basePackages = {"name.your.package.here"}
public class AppConfig
{
@Bean // or @Bean(name = "nameOfYourBean")
public ResourcePool getResourcePool()
{
return new ResourcePool();
}
}
在你的主要方法,你应该再在你的环境中创建一个Spring上下文并加载您的AppConfig:
Within your main method you should then create a Spring context and load your AppConfig in to your context:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfic.class);
心连心
这篇关于如何使用Spring 3.X注解没有任何.xml配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!