gpt4 book ai didi

java - 为什么初始化springboot时会出现调用bean失败的错误

转载 作者:行者123 更新时间:2023-12-02 04:18:59 29 4
gpt4 key购买 nike

我正在尝试在类里面设置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 INFO 29037 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2019-06-18 10:58:02.661 INFO 29037 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2019-06-18 10:58:02.667 ERROR 29037 --- [ main] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'unitAmqpRestController': Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar!/:1.5.10.RELEASE] at com.infy.ci.unitamqpservice.UnitamqpserviceApplication.main(UnitamqpserviceApplication.java:10) [classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [unitamqpservice-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] Caused by: java.lang.NullPointerException: null at com.infy.ci.unitamqpservice.UnitAmqpRestController.main(UnitAmqpRestController.java:69) ~[classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134) ~[spring-beans-4.3.14.RELEASE.jar!/:4.3.14.RELEASE] ... 26 common frames omitted

不知道问题是在构造函数中还是在主方法中?我是否缺少 @Bean 配置之类的东西??

请提出建议

最佳答案

感谢 M.Deinum 的评论和帖子。我引用了

上的详细文档

spring-boot-starter-amqp

https://spring.io/guides/gs/messaging-rabbitmq/

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-messaging.html#boot-features-amqp

它清楚地提到了如何不需要我自己处理rabbitmq连接工厂。通过在 application.properties 文件中定义标准关键参数,AmQPTemplate 可以完成所有操作。

谢谢

关于java - 为什么初始化springboot时会出现调用bean失败的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648197/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com