当前位置: 首页 > 知识库问答 >
问题:

创建名为sendController的bean时出错

凌善
2023-03-14

我遵循本教程将消息发送到azure服务队列:https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-service-bus

到我现有的spring boot应用程序,但我得到以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sendController': Unsatisfied dependency expressed through field 'jmsTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jmsTemplate' defined in class path resource [com/microsoft/azure/spring/autoconfigure/jms/ServiceBusJMSAutoConfiguration.class]: Unsatisfied dependency expressed through method 'jmsTemplate' parameter 0; nested exception is 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

用户类别:

    package com.proyecto.demo.domain;

import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;

import java.io.Serializable;

import org.springframework.data.annotation.Id;

@Document(collection = "tUser")
public class User implements Serializable {

    private static final long serialVersionUID = -295422703255886286L;

    @Id
    private String id;
    private String firstName;

    @PartitionKey
    private String lastName;
    private String address;

    public User(String id, String firstName, String lastName, String address) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
    }

    public User(String firstName) {
        this.firstName = firstName;
    }

    public User() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return String.format("%s %s, %s", firstName, lastName, address);
    }
}

控制器类:

    package com.proyecto.demo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.proyecto.demo.domain.User;

@RestController
public class SendController {

    private static final String DESTINATION_NAME = "testqueue";

    @Autowired
    private JmsTemplate jmsTemplate;

    @PostMapping("/messages")
    public String postMessage(@RequestParam String message) {

        jmsTemplate.convertAndSend(DESTINATION_NAME, new User(message));
        return message;
    }

}

pom xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
     instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven- 
      4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.proyecto</groupId>
    <artifactId>FileUploaderProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>FileUploaderProject</name>
    <description>FileUploader</description>

    <properties>
        <java.version>1.8</java.version>
        <azure.version>2.2.4</azure.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-servicebus-jms-spring-boot-starter</artifactId>

        </dependency>

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-cosmosdb-spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-spring-boot-bom</artifactId>
                <version>${azure.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

添加它使用的应用程序属性就像yml我删除了所有敏感信息

 azure:
  cosmosdb:
    database: testdb
    key: 
    uri: https://testcosmosql.documents.azure.com:443/
spring:
  jms:
    servicebus:
      connection-string:
      idle-timeout: 1800000

以下是一些堆栈跟踪:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sendController': Unsatisfied dependency expressed through field 'jmsTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jmsTemplate' defined in class path resource [com/microsoft/azure/spring/autoconfigure/jms/ServiceBusJMSAutoConfiguration.class]: Unsatisfied dependency expressed through method 'jmsTemplate' parameter 0; nested exception is 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at com.proyecto.demo.FileUploaderProjectApplication.main(FileUploaderProjectApplication.java:11) ~[classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jmsTemplate' defined in class path resource [com/microsoft/azure/spring/autoconfigure/jms/ServiceBusJMSAutoConfiguration.class]: Unsatisfied dependency expressed through method 'jmsTemplate' parameter 0; nested exception is 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

我尝试了不同的解决方案,如禁用:ActiveMQAutoConfiguration和我发现的其他一些解决方案,但没有一个。

我尝试了很多在堆栈上找到的解决方案,这和jms未实例化的事实有关。我将JMS添加到我的pom文件中。

我也尝试了更新版本我已经尝试了很多不同的解决方案但它仍然不工作

我也试过这个:

UnsatisfiedDependencyException:创建名为的bean时出错

非常感谢你的帮助

共有2个答案

贲招
2023-03-14

非常感谢您的帮助,Arthur是对的,连接字符串有问题,我有一个连接字符串:‘‘‘constitng’

闾丘鸣
2023-03-14

您遇到的问题是Spring无法实例化您指定的自动连接类。

Error creating bean with name 'jmsConnectionFactory' defined in class path resource

意味着,JmsConnectionFactorybean没有实例化。我没有在你的pom.xml.中找到JMS库

您可以通过将以下内容添加到您的pom.xml来包含JMS库

<dependency>
    <groupId>javax.jms</groupId>
    <artifactId>javax.jms-api</artifactId>
    <version>2.0.1</version>
</dependency>

然后spring可以实例化JmsConnectionFactory。

 类似资料:
  • 我有一个实体类InAppNotification。看起来像这样的java: 我使用JPA来处理数据库查询,这就是JPA接口的定义: 这是我application.properties的样子: 但是,当我试图在构建后通过运行 来打包应用程序时,我会遇到以下问题: 尝试调用不存在的方法。尝试从以下位置进行:javax.el.ELManager.getExpress sionWorks(ELManage

  • 在将project从Spring Boot版本从1.2.3.release迁移到1.3.0.release之后,我已经开始得到以下异常。 创建类路径资源[org/springframework/boot/autoconfigure/admin/springapplicationadminjmxautoconfiguration.class]中定义的名为'Spring ApplicationAdmi

  • 我试图将弹性搜索集成到spring-boot应用程序中,但我得到了这个*创建名为“client”的bean时出错*异常,不确定是什么地方出了问题,因为我之前找不到任何类似的线索...非常感谢你为大家指路。这是mu elasticsearch配置: 这是我的主要应用程序: 我的pom.xml: 这是te异常跟踪:

  • 我有这些错误编译,我不知道我哪里错了,我只有这3个类

  • 我正在尝试在spring boot应用程序中使用SQLite。但是应用程序不能创建下面的bean。 org.springframework.boot.autocigure.orm.jpa.hibernatejaconfiguration 我该怎么办?我在这个站点上搜索了相关的问题,但是找不到一个合适的。 如下所示。 4.0.0 org.springframework.Boot spring-boo

  • 在我的Spring应用程序中,我有这些问题。有人能帮我吗? 我在pom.xml上添加了一些东西,但是应用程序没有启动,有很多错误。 启动应用程序上下文时出错。若要显示条件报告,请在启用“调试”的情况下重新运行应用程序。2019-01-26 14:58:17.003 错误 14580 --- [ 重新启动主屏幕] o.s.boot.Spring 应用程序: 应用程序运行失败