Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确配置@Component类_Java_Spring_Spring Boot_Dependency Injection - Fatal编程技术网

Java 如何正确配置@Component类

Java 如何正确配置@Component类,java,spring,spring-boot,dependency-injection,Java,Spring,Spring Boot,Dependency Injection,我有一个SqsQueueSender向AWS发送消息。我想考这门课。我的想法是,它应该是一个@组件,注入到需要它的类中。重要的是,我想将sqsqsquesender的端点配置为在测试和生产环境中有所不同 我一直以各种不同的方式在类中移动@Autowired和@Component,但肯定有一些基本的误解。以下是我的最新配置: package com.foo.fulfillmentApi.dao; import com.amazonaws.services.sqs.AmazonSQSAsyncCl

我有一个SqsQueueSender向AWS发送消息。我想考这门课。我的想法是,它应该是一个
@组件
,注入到需要它的类中。重要的是,我想将
sqsqsquesender
的端点配置为在测试和生产环境中有所不同

我一直以各种不同的方式在类中移动@Autowired和@Component,但肯定有一些基本的误解。以下是我的最新配置:

package com.foo.fulfillmentApi.dao;

import com.amazonaws.services.sqs.AmazonSQSAsyncClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.messaging.support.MessageBuilder;

@Component
public class SqsQueueSender {

    private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSender.class);
    private final QueueMessagingTemplate queueMessagingTemplate;

    @Autowired
    AmazonSQSAsyncClient amazonSQSAsyncClient;

    //This value is from /resources/application.properties
    private @Value("${sqs.endpoint}") String endpoint;

    public SqsQueueSender(AmazonSQSAsyncClient amazonSqsAsyncClient) {
        amazonSqsAsyncClient.setEndpoint(endpoint);
        this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqsAsyncClient);
    }

    public void send(String queueName, String message) {
        this.queueMessagingTemplate.convertAndSend(queueName, MessageBuilder.withPayload(message).build());
    }
}
启动状态下的错误消息

原因: org.springframework.beans.factory.noSuchBean定义异常:否 类型的限定bean “com.amazonaws.services.sqs.amazonsqasyncClient”可用:应为 至少1个符合autowire候选资格的bean。附属国 注释:{}


要实现
sqsqsquesender
,必须传递
amazonsqasyncClient
。如何确保此组件可以访问该类型的现有bean?

在com.amazonaws.services.sqs.amazonsqasyncClient中添加@component/@Service,或使用配置类中的@bean注释返回该类型的对象。

您需要创建一个配置类。在您的情况下,可能是这样的:

@Configuration
public class AWSConfig {

   @Bean(name ="awsClient")
   public AmazonSQSAsync amazonSQSClient() {
     AmazonSQSAsyncClient awsSQSAsyncClient 
            = new AmazonSQSAsyncClient();

     // set up the client

     return awsSQSAsyncClient;
}
如果注入有问题,则在qsQueueSender中添加限定符:

@Autowired
@Qualifier("awsClient")
AmazonSQSAsyncClient amazonSQSAsyncClient;

您也可以使用xml配置来实现这一点,但由于您使用的是注释,因此这是更可取的方法。

如果您使用springboot,请在启动应用程序文件中定义,如下所示

@Bean
    public AmazonSNSAsync amazonSNSClient() {
        ClientConfiguration config = new ClientConfiguration();
        return AmazonSNSAsyncClient.asyncBuilder().withClientConfiguration(config)
                .withRegion(Regions.fromName(region))
                .withCredentials(new DefaultAWSCredentialsProviderChain())
                .build();

    }

我对aws一无所知。但是
amazonsqsayncclient
定义是一个bean在哪里?如果我理解你的问题,在我的项目中该类并没有定义为
@bean
。如何从已导入的现有类中创建@Bean?Re:“使用配置类中的@Bean注释返回该类的对象”。这是否意味着创建一个用@Bean注释的类,该类描述如何构造
AmazonSQSAsyncClient
?如果AmazonSQSAsyncClient是一个具体的类,我该如何实现它?谢谢。我的应用程序现在启动。我仍然无法对使用
客户端的类运行测试。如何在测试类中实例化
sqsqsquesender
的实例?。阅读使用@Configuration Classes进行集成测试部分。对你来说应该够了,恐怕还不够。如果您感兴趣,我已将更多详细信息转发给您:。再次感谢,非常有帮助。