我正在尝试在课堂上设置Rabbitmq连接工厂。想使用@Value注释读取application.properties文件中定义的属性。但是,某种程度上bean配置不正确。我不明白这一点。 这是代码:
@Controller
@Configuration
@RequestMapping("/unittestdata")
@Component
@PropertySource("classpath:application.properties")
public class UnitAmqpRestController {
@Value("${spring.rabbitmq.port}")
public static int mqPort;
public UnitAmqpRestController() {
}
private static final Logger logger = LoggerFactory.getLogger(UnitAmqpRestController.class);
private static RabbitTemplate rabbitTemplate;
private String replyQueueName;
private static Connection connection;
private static Channel channel;
private String requestQueueName = "rpc_queue_unit";
public UnitAmqpRestController(RabbitTemplate rabbitTemplate, String replyQueueName, Connection connection, Channel channel, String requestQueueName) {
this.rabbitTemplate = rabbitTemplate;
this.replyQueueName = replyQueueName;
this.connection = connection;
this.channel = channel;
this.replyQueueName = requestQueueName;
}
@PostConstruct
public static void main() throws Exception {
rabbitTemplate.setReplyTimeout(15_000L);
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(getHostProperty());
factory.setPort(mqPort);
connection = factory.newConnection();
channel = connection.createChannel();
}
public static String getHostProperty() {
Properties properties = new Properties();
try {
File file = ResourceUtils.getFile("classpath:application.properties");
InputStream in = new FileInputStream(file);
properties.load(in);
} catch (IOException e) {
}
return properties.getProperty("spring.rabbitmq.host");
}
这是错误:
2019-06-18 10:58:02.642信息29037-[[主要] o.apache.catalina.core.StandardService:正在停止服务[Tomcat] 2019-06-18 10:58:02.661信息29037-[[主要] utoConfigurationReportLoggingInitializer:
启动ApplicationContext时出错。显示自动配置 报告在启用“调试”的情况下重新运行您的应用程序。 2019-06-18 10:58:02.667错误29037 --- [main] o.s.boot.SpringApplication:应用程序启动失败
org.springframework.beans.factory.BeanCreationException:错误 创建名称为“ unitAmqpRestController”的bean:调用init 方法失败;嵌套的异常是java.lang.NullPointerException 在org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)处 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)处 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) 〜[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 〜[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) 〜[spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] 在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] 在com.infy.ci.unitamqpservice.UnitamqpserviceApplication.main(UnitamqpserviceApplication.java:10) [课程!/:0.0.1-SNAPSHOT] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_212] 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[na:1.8.0_212] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 〜[na:1.8.0_212] 在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_212] 在org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] 在org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] 在org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] 在org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]由以下原因引起: java.lang.NullPointerException:空 在com.infy.ci.unitamqpservice.UnitAmqpRestController.main(UnitAmqpRestController.java:69) 〜[课程!/:0.0.1-SNAPSHOT] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_212] 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[na:1.8.0_212] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 〜[na:1.8.0_212] 在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_212] 在org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor $ LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)处 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor $ LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)处 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] 在org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)处 〜[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] ...省略了26个共同的框架
没有得到问题是在构造函数中还是在main方法中? 我是否缺少@Bean配置之类的东西?
请建议
答案 0 :(得分:0)
感谢M.Deinum的评论和帖子。 我在
上提到了详细的文档spring-boot-starter-amqp
https://spring.io/guides/gs/messaging-rabbitmq/
它清楚地提到了如何不必自己处理Rabbitmq连接工厂。通过在application.properties文件中定义标准关键参数,AmQPTemplate可以完成所有工作。
谢谢