【问题标题】:Error creating bean with name 'jmsConnectionFactory' - NullPointerException - Spring Boot Kotlin创建名为“jmsConnectionFactory”的 bean 时出错 - NullPointerException - Spring Boot Kotlin
【发布时间】:2021-01-21 03:14:27
【问题描述】:

我正在编写一个报告服务来接收来自 Azure 服务总线主题订阅的消息。 我遵循了这个初始指南 - How to use the Spring Boot Starter for Azure Service Bus JMS 并在我的服务文件中简单地配置了 @JmsListener 以不指定 JmsConnectionFactory 以便它恢复到默认值,但是这返回了下面显示的错误日志,jmsConnectionFactory Bean 返回NullPointerException

我随后查看了Bauldung - Getting Started with Spring JMS Baeldung - Messaging with JMS 以获取有关如何配置jmsConnectionFactory 的指导,当初始实现无法运行但对上述文章中提到的任何建议都没有运气时,因为它仍然存在返回下面的错误日志。

然后我在 StackOverflow 上进行了挖掘,发现了这个问题 - Spring Boot - Error creating bean with name 'jmsConnectionFactory',然后指向了这个问题 - Disabling Spring JMS Auto configuration in Spring Boot Application,这导致我在我的 @SpringBootApplication 中排除了 ServiceBusJMSAutoConfiguration::class。这确实有效,但我觉得我需要排除这个类有点奇怪,并且感觉好像还有其他事情我可能做错了。

我基本上是在寻找任何关于我正在做的是否是正常练习或者是否有更好的方法来进行此设置的见解,因为我并不是特别喜欢这个 - @SpringBootApplication(exclude = [ServiceBusJMSAutoConfiguration::class]) .对我来说,这似乎是解决潜在问题的某种技巧。对此的任何帮助/意见将不胜感激。

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

ReportingServiceApplication.kt

@SpringBootApplication(exclude = [ServiceBusJMSAutoConfiguration::class])
class ReportingServiceApplication

fun main(args: Array<String>) {
    runApplication<ReportingServiceApplication>(*args)
}

示例监听器 - AwardEditEventService.kt

interface AwardEditEventService {
    fun receiveMessage(awardEditEvent: AwardEditEvent)
}

@Service
class AwardEditEventServiceImpl(
    val awardEditEventsRepository: AwardEditEventRepository
) : AwardEditEventService {

    @JmsListener(
        destination = "reporting",
        subscription = "AwardEdits"
    )
    override fun receiveMessage(awardEditEvent: AwardEditEvent) {
        awardEditEventsRepository.save(awardEditEvent)
    }
}

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.3.4.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    id("com.diffplug.gradle.spotless") version "4.0.1"
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"
    kotlin("plugin.jpa") version "1.3.72"
}

group = "com.test.service"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {

    // Kotlin
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // Spring Boot
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")

    // Flyway
    implementation("org.flywaydb:flyway-core")

    // App Insights
    implementation("com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.1")
    implementation("com.microsoft.azure:applicationinsights-logging-logback:2.6.1")

    // Azure Service Bus
    implementation("com.microsoft.azure:azure-servicebus-spring-boot-starter:2.2.5")
    implementation("com.microsoft.azure:azure-servicebus-jms-spring-boot-starter:2.2.5")

    // Snowflake
    implementation("net.snowflake:snowflake-jdbc:3.12.12")

    // Postgres (For Local Development)
    runtimeOnly("org.postgresql:postgresql")

    // Test
    testImplementation("com.h2database:h2:1.4.200")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("com.nhaarman:mockito-kotlin:1.6.0")
    testImplementation("org.hamcrest:hamcrest-library:2.2")
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

这是初始错误的日志 -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmsConnectionFactory' defined in class path resource [com/microsoft/azure/spring/autoconfigure/jms/ServiceBusJMSAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.jms.ConnectionFactory]: Factory method 'jmsConnectionFactory' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
    at com.test.service.reporting.ReportingServiceApplicationKt.main(ReportingServiceApplication.kt:13) ~[main/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.jms.ConnectionFactory]: Factory method 'jmsConnectionFactory' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.microsoft.azure.spring.autoconfigure.jms.ConnectionStringResolver.getServiceBusKey(ConnectionStringResolver.java:30) ~[azure-spring-boot-2.2.5.jar:na]
    at com.microsoft.azure.spring.autoconfigure.jms.ServiceBusJMSAutoConfiguration.jmsConnectionFactory(ServiceBusJMSAutoConfiguration.java:40) ~[azure-spring-boot-2.2.5.jar:na]
    at com.microsoft.azure.spring.autoconfigure.jms.ServiceBusJMSAutoConfiguration$$EnhancerBySpringCGLIB$$9ce4d56.CGLIB$jmsConnectionFactory$3(<generated>) ~[azure-spring-boot-2.2.5.jar:na]
    at com.microsoft.azure.spring.autoconfigure.jms.ServiceBusJMSAutoConfiguration$$EnhancerBySpringCGLIB$$9ce4d56$$FastClassBySpringCGLIB$$99b2393f.invoke(<generated>) ~[azure-spring-boot-2.2.5.jar:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    at com.microsoft.azure.spring.autoconfigure.jms.ServiceBusJMSAutoConfiguration$$EnhancerBySpringCGLIB$$9ce4d56.jmsConnectionFactory(<generated>) ~[azure-spring-boot-2.2.5.jar:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
    ... 21 common frames omitted

【问题讨论】:

    标签: spring-boot jms spring-jms


    【解决方案1】:

    您需要为服务总线指定定价层,如下所示:

    spring:
      jms:
        servicebus:
          connection-string: Endpoint=sb://xxx
          idle-timeout: 30000
          pricing-tier: Standard
    

    【讨论】:

      【解决方案2】:

      spring.jms.servicebus.pricing-tier = XXXXX 在您的配置中非常重要。您应该将 XXXXX 替换为您的定价层。

      【讨论】:

        猜你喜欢
        • 2019-02-12
        • 2019-02-27
        • 2023-02-09
        • 2018-10-27
        • 2020-06-05
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 2018-01-25
        相关资源
        最近更新 更多