インスタンス変数を使用してBeanを初期化しようとしています。だから私は、queueNameとmessageListener与えられたキューを作成するための情報を含む基本クラスがあります。スプリングブート。インスタンス変数を使用したBeansの初期化
豆が初期化されていません。誰かが私のコードを見て助けてくれますか?他のクラスがコードの二重性を減らすために使用できるクラスを作成しようとしました。そのため、インスタンス変数を使用してBeanを作成しています。
//class Application
package com.ats.myntra;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
//class RabbitMqQueue
import javax.annotation.PostConstruct;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqQueue {
private String queue;
private MessageListener messageListener;
public RabbitMqQueue(String queue, MessageListener messageListener){
this.queue = queue;
this.messageListener = messageListener;
}
@Bean
MessageListenerAdapter listenerAdapter() {
return new MessageListenerAdapter(messageListener, "onMessage");
}
@PostConstruct
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queue);
container.setMessageListener(messageListener);
return container;
}
@Bean
Queue queue() {
return new Queue(queue, false);
}
}
//class q1
package Configuration;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;
@Component
public class q1 implements MessageListener{
q1 q = new q1();
String queue = "abcd";
RabbitMqQueue rabbitMqQueue = new RabbitMqQueue(queue,q);
static void print(){
System.out.println("why");
}
//rabbitMqQueue =
@Override
public void onMessage(Message message) {
// TODO Auto-generated method stub
}
}
を支援キュー。したがって、将来キューを追加する必要がある場合は、このクラスを変更する必要はありません。 – user1973987