SpringBootApplication类内部的SpringAutowire在几乎相同的应用程序之间表现不同

SpringBootApplication类内部的SpringAutowire在几乎相同的应用程序之间表现不同,spring,rabbitmq,spring-boot,Spring,Rabbitmq,Spring Boot,我有两个SpringBoot应用程序,其中我使用RabbitMQ跨队列通信消息。一个应用程序用于发送消息,另一个用于侦听已发送的消息。每个应用程序都包含一个@SpringBootApplication文件,该文件在属性级别上有一个@Autowired依赖项(一个应用程序有发送方,另一个应用程序有侦听方),每个应用程序都有一个单独的Spring@配置文件,每个文件声明一个bean(一个有发送方,一个有接收方) 出于某种原因,发送方应用程序对注入没有问题,但是接收方应用程序没有在@Autowire处

我有两个SpringBoot应用程序,其中我使用RabbitMQ跨队列通信消息。一个应用程序用于发送消息,另一个用于侦听已发送的消息。每个应用程序都包含一个@SpringBootApplication文件,该文件在属性级别上有一个@Autowired依赖项(一个应用程序有发送方,另一个应用程序有侦听方),每个应用程序都有一个单独的Spring@配置文件,每个文件声明一个bean(一个有发送方,一个有接收方)

出于某种原因,发送方应用程序对注入没有问题,但是接收方应用程序没有在@Autowire处注入,即使bean在我的应用程序上下文中。我使用示例应用程序向我们的公司演示RabbitMQ/SpringAMQP与SpringBoot和MicroService。下面是发送方应用程序的代码,后跟接收方应用程序。如果我将Receiver应用程序更改为使用Setter注入,它就可以正常工作,我只是很好奇为什么其中一个可以工作,而另一个不能。Receiver应用程序在其主方法中调用Receiver.receive()时崩溃,如下所示:

线程“main”java.lang.NullPointerException中出现异常 位于com.bettercloud.springamqpplication.main(springamqpplication.java:17)

接收器应用程序:

@SpringBootApplication
public class SpringAmqpApplication {

@Autowired
static Recv receiver;
public static void main(String[] args) throws IOException,InterruptedException {
    SpringApplication.run(SpringAmqpApplication.class, args);
    receiver.receive();
  }
 }

@Configuration
public class Config {

@Bean
public Recv recv(){
    return new Recv();
 }
 }


 public class Recv {

 private final static String QUEUE_NAME = "task_queue";
 public void receive()
        throws java.io.IOException,
        InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());

        System.out.println(" [x] Received '" + message + "'");
        doWork(message);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
  }

private static void doWork(String task) throws InterruptedException {
    for (char ch: task.toCharArray()) {
        if (ch == '.') Thread.sleep(1000);
    }
  }
}
@SpringBootApplication
public class SpringAmqpProducerApplication {

@Autowired
static Send sender;

public static void main(String[] args) throws IOException {
    SpringApplication.run(SpringAmqpProducerApplication.class, args);
    sender.send(null);
   }
 }


@Configuration
public class Config {

@Bean
public Send send(){
    return new Send();
    }
 }

public class Send {

private final static String QUEUE_NAME = "task_queue";

public static void send(String[] argv)
        throws java.io.IOException {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        String message = getMessage(argv);
        channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
    } finally {
        channel.close();
        connection.close();
     }
  }

private static String getMessage(String[] strings){
    if (strings == null || strings.length < 1)
        return "Hello World!";
    return joinStrings(strings, " ");
}

private static String joinStrings(String[] strings, String delimiter) {
    int length = strings.length;
    if (length == 0) return "";
    StringBuilder words = new StringBuilder(strings[0]);
    for (int i = 1; i < length; i++) {
        words.append(delimiter).append(strings[i]);
    }
    return words.toString();
 }
}
发件人应用程序:

@SpringBootApplication
public class SpringAmqpApplication {

@Autowired
static Recv receiver;
public static void main(String[] args) throws IOException,InterruptedException {
    SpringApplication.run(SpringAmqpApplication.class, args);
    receiver.receive();
  }
 }

@Configuration
public class Config {

@Bean
public Recv recv(){
    return new Recv();
 }
 }


 public class Recv {

 private final static String QUEUE_NAME = "task_queue";
 public void receive()
        throws java.io.IOException,
        InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());

        System.out.println(" [x] Received '" + message + "'");
        doWork(message);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
  }

private static void doWork(String task) throws InterruptedException {
    for (char ch: task.toCharArray()) {
        if (ch == '.') Thread.sleep(1000);
    }
  }
}
@SpringBootApplication
public class SpringAmqpProducerApplication {

@Autowired
static Send sender;

public static void main(String[] args) throws IOException {
    SpringApplication.run(SpringAmqpProducerApplication.class, args);
    sender.send(null);
   }
 }


@Configuration
public class Config {

@Bean
public Send send(){
    return new Send();
    }
 }

public class Send {

private final static String QUEUE_NAME = "task_queue";

public static void send(String[] argv)
        throws java.io.IOException {
    Connection connection = null;
    Channel channel = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        String message = getMessage(argv);
        channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
    } finally {
        channel.close();
        connection.close();
     }
  }

private static String getMessage(String[] strings){
    if (strings == null || strings.length < 1)
        return "Hello World!";
    return joinStrings(strings, " ");
}

private static String joinStrings(String[] strings, String delimiter) {
    int length = strings.length;
    if (length == 0) return "";
    StringBuilder words = new StringBuilder(strings[0]);
    for (int i = 1; i < length; i++) {
        words.append(delimiter).append(strings[i]);
    }
    return words.toString();
 }
}
@springboot应用程序
公共类SpringAMQProducerApplication{
@自动连线
静态发送发送方;
公共静态void main(字符串[]args)引发IOException{
run(springamqproducerapplication.class,args);
sender.send(空);
}
}
@配置
公共类配置{
@豆子
公共发送{
返回新的Send();
}
}
公共类发送{
私有最终静态字符串队列\u NAME=“任务\u队列”;
公共静态无效发送(字符串[]argv)
抛出java.io.IOException{
连接=空;
通道=空;
试一试{
ConnectionFactory工厂=新的ConnectionFactory();
setHost(“localhost”);
connection=factory.newConnection();
channel=connection.createChannel();
queueDeclare(队列名称,true,false,false,null);
String message=getMessage(argv);
channel.basicPublish(“”,队列名称,MessageProperties.PERSISTENT\u TEXT\u PLAIN,message.getBytes());
System.out.println(“[x]已发送”“+message+””);
}最后{
channel.close();
connection.close();
}
}
私有静态字符串getMessage(字符串[]字符串){
if(strings==null | | strings.length<1)
返回“你好,世界!”;
返回JoinString(字符串“”);
}
私有静态字符串JoinString(字符串[]字符串,字符串分隔符){
int length=strings.length;
如果(长度==0)返回“”;
StringBuilder words=新的StringBuilder(字符串[0]);
for(int i=1;i
我认为注入根本不起作用,因为您试图注入静态字段,这对Spring不起作用。从字段(以及方法)中删除静态标识符,因为没有理由它们也是静态的,所以应用程序应该可以正常工作


发送方可以工作,因为send方法是静态的,所以不需要对象来调用该方法。

不确定这是否有帮助,但是send类中的send()方法是静态的,而receive类的receive()方法不是