首页 > 解决方案 > Autowire 不适用于 org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties

问题描述

我正在尝试在 SpringBoot 应用程序中使用的库中自动装配 org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties,但出现找不到 bean 类型的错误。在构建期间,我没有任何问题。

该库是我想从 ArtemisProperties 类中检索一些值。这个库中的 pom.xml 导入了 artemis 库。

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xxx.ems.microservice.core</groupId>
  <artifactId>core-library</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>CoreLibrary</name>
  <url>http://maven.apache.org</url>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
  </parent>  


  <properties>
      <maven.compiler.target>1.8</maven.compiler.target>
      <maven.compiler.source>1.8</maven.compiler.source>
  </properties>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <exclusions>
              <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
          <exclusions>
              <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-artemis</artifactId>
      </dependency>
      <dependency>
          <groupId>org.apache.activemq</groupId>
          <artifactId>artemis-jms-server</artifactId>
      </dependency>
      <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
      </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>           
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <scope>test</scope>
      </dependency>

        <dependency>
            <groupId>co.elastic.apm</groupId>
            <artifactId>apm-agent-api</artifactId>
            <version>1.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>   
        <dependency>
            <groupId>com.jcabi</groupId>
            <artifactId>jcabi-manifests</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
  </dependencies>
</project>

在图书馆中,我有一个类,我想在其中获得 Artemis 尝试连接的主机的一些值

package com.xxx.ems.microservice.core.queue;

import java.util.regex.PatternSyntaxException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import com.xxx.ems.microservice.core.queue.MessageDispatcherConfiguration.DispatchInfo;

import co.elastic.apm.api.CaptureSpan;
import lombok.extern.slf4j.Slf4j;

// Message Dispatcher
@Component
@EnableJms
@Slf4j
public class MessageDispatcher {
    @Autowired
    MessageDispatcherConfiguration messageDispatcherConfiguration;

    @Autowired
    MessageSender messageSender;

   @Autowired
   private ArtemisProperties artemisProperties;



    private void sendFile(DispatchInfo dispatcheInfo, MessageInformationExtractor messageInformationExtractorForDispatch, EMSFileMessage emsFileMessage, int ... priority) throws EMSQueueException{
        if (hasToBeSend(dispatcheInfo, messageInformationExtractorForDispatch, emsFileMessage)){
            for (String destinationQueue:dispatcheInfo.getDestinationQueueNames()){ 
                log.debug("Will send {} to {}:{}  and queue {}", emsFileMessage.getId(), artemisProperties.getHost(), artemisProperties.getPort(), destinationQueue);
                messageSender.send(destinationQueue, emsFileMessage, priority); 
            } ;
        }
    }
....        

在我的 spring-boot 应用程序 pom.xml 中,我包含了与库的依赖关系,并且包含了对库中类的组件扫描:

package com.xxx.ems.microservice.msdiskloader;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import com.xxx.ems.microservice.core.app.EMSMicroserviceApplication;

@SpringBootApplication
@ComponentScan("com.xxx.ems")
@Configuration  
@EnableAsync 
public class MSDiskLoaderApplication extends EMSMicroserviceApplication{

    public static void main(String[] args) {
        EMSMicroserviceApplication.run(MSDiskLoaderApplication.class, args);
    }

}

任何线索为什么我有这个自动接线错误?已正确添加了来自 artemis 库的依赖项。可能与其他库不兼容吗?

完整的错误如下

**************************
APPLICATION FAILED TO START
***************************

Description:

Field artemisProperties in com.xxx.ems.microservice.core.queue.MessageDispatcher required a bean of type 'org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.jms.artemis.ArtemisProperties' in your configuration.

谢谢

标签: spring-bootautowiredactivemq-artemis

解决方案


@ConfigurationProperties并且自动配置不是公共 API,所以我强烈建议您不要在自己的代码中使用它。该 bean 是由自动配置创建的,如果您的自定义代码使其回退,则不会再创建它。

@EnableConfigurationProperties(ArtemisProperties.class)是确保它在上下文中注册并绑定到环境的最佳方法。


推荐阅读