我正在尝试使用实例变量初始化bean。所以我有基类,它包含给定queueName和messageListener创建队列的信息。
bean没有初始化。有人可以查看我的代码和帮助。我的目标是创建其他类可以用来减少代码重复性的类。这就是我使用实例变量来创建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
}
}
答案 0 :(得分:0)
问题在于您的配置类。不要对这些值使用构造函数注入,而是直接在字段上注入。我修改了您的示例,假设您的配置文件中有一个名为“queue.name”的属性,但您可以将其更改为您正在使用的任何属性。
@Configuration
public class RabbitMqQueue {
@Value("${queue.name}")
private String queue;
@Autowired
private 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);
}
}
或者,您实际上只需将值注入bean工厂方法,如下所示:
//snip
@Bean
@Autowired
MessageListenerAdapter listenerAdapter(private MessageListener messageListener) {
return new MessageListenerAdapter(messageListener, "onMessage");
}
//snip
@Bean
Queue queue(@Value("${queue.name}" String queueName){
return new Queue(queue, false);
}
//snip
希望有所帮助:)