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

spring boot@Autowired中的多个配置属性(其中一个属性为空)

左丘季
2023-03-14

我刚开始使用spring,我有以下引导应用程序类。我正在尝试从Spring Boot应用程序连接到AWS SQS。代码如下:

@SpringBootApplication
@EnableConfigurationProperties ({ApplicationProperties.class, AwsProperties.class})
public class Application{
    private static final Logger logger = LoggerFactory.getLogger(Application.class);    
    public static void main(String[] args) throws IOException {
        SpringApplication.run(Application.class, args);
    }
}

ApplicationProperties.java

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix="midb")
public class ApplicationProperties {

    private String keyStore;
    private String keyStorePassword;

// getter and setters
}

AwsProperties.java

@Configuration
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix="aws")
public class AwsProperties {

    private String sqsEndpoint;
    private String accessKey;
    private String secretKey;
// getters and setters
}


@Configuration
@EnableJms
@EnableConfigurationProperties(AwsProperties.class)
public class JmsConfig {

    private static final Logger logger = LoggerFactory.getLogger(JmsConfig.class);


    @Autowired
    private AwsProperties awsProperties;

    @Autowired
    private SQSListener sqsListener;
    @PostConstruct
    public void init() {
        //System.out.println("================== " + awsProperties.toString() + "==================");// End point:"+endpoint);
    }

    @Bean
    public AmazonSQSClient createSQSClient() {

        AmazonSQSClient amazonSQSClient = new AmazonSQSClient(new BasicAWSCredentials(awsProperties.getAccessKey(), awsProperties.getSecretKey()));
        amazonSQSClient.setEndpoint(awsProperties.getSqsEndpoint());
        amazonSQSClient.createQueue(awsProperties.getSqsQueueName());
        return amazonSQSClient;
    }

    @Bean
    public DefaultMessageListenerContainer jmsListenerContainer() {
        SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
                .withAWSCredentialsProvider(new DefaultAWSCredentialsProviderChain())
                .withEndpoint(awsProperties.getSqsEndpoint()).withAWSCredentialsProvider(awsCredentialsProvider)
                .withNumberOfMessagesToPrefetch(10).build();
        DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
        dmlc.setConnectionFactory(sqsConnectionFactory);
        dmlc.setDestinationName(awsProperties.getSqsQueueName());
        dmlc.setMessageListener(sqsListener);
        return dmlc;
    }

    @Bean
    public JmsTemplate createJMSTemplate() {
        SQSConnectionFactory sqsConnectionFactory = SQSConnectionFactory.builder()
                .withAWSCredentialsProvider(awsCredentialsProvider).withEndpoint(awsProperties.getSqsEndpoint())
                .withNumberOfMessagesToPrefetch(10).build();
        JmsTemplate jmsTemplate = new JmsTemplate(sqsConnectionFactory);
        jmsTemplate.setDefaultDestinationName(awsProperties.getSqsQueueName());
        jmsTemplate.setDeliveryPersistent(false);
        return jmsTemplate;
    }

    private final AWSCredentialsProvider awsCredentialsProvider = new AWSCredentialsProvider() {
        @Override
        public AWSCredentials getCredentials() {
            return new BasicAWSCredentials(awsProperties.getAccessKey(), awsProperties.getSecretKey());
        }

        @Override
        public void refresh() {
        }
    };
}

当Maven构建时,我得到以下错误:

原因:org.springframework.beans.factory.BeanCreationException:创建类路径资源[io/bigbear/midb/sqs/jmsconfig.class]中定义的名为“create sqsclient”的bean时出错:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanInstantiationException:无法实例化[com.amazonaws.services.sqs.amazonsqsclient]:工厂方法“create sqsclient”引发异常;嵌套异常为java.lang.IllegalArgumentException:访问键不能为空。

共有1个答案

柴默
2023-03-14

我不确定,但您的awsProperties.getAccessKey()似乎返回NULL。

 类似资料:
  • 寻找在Springboot应用程序中配置多个配置文件特定属性文件的最佳方法。下面是一个例子: -资源  · --application.properties  · · · · · --德夫             --application-dev.properties             --ldap-dev.properties             --Quartz-Dev.Prope

  • 我试图一次为一个元素设置多个属性。我找到了这个答案,以及对那个答案的这个评论。在那里的JSFiddle中,他不使用字符串作为属性名,与使用字符串的答案相反。 我在第7行添加了以下内容: 但是我得到了以下错误: 未捕获的引用Error:未定义html 评论JSFiddle(已编辑)

  • 问题内容: 我将sql存储在属性文件中,并使用spring注入它,这有效: 但出于可读性,我想要这样: 我需要使用什么正确的文本格式? 问题答案: 在行的末尾使用\ 另外,请注意任何尾随空格,因为Java在组装线时会寻找连续的反斜杠+换行符。 换种说法:反斜线必须是换行符之前该行的最后一个字符。

  • 我们使用的是Spring boot。我们有三个环境、、。我们当前的配置结构 开发 同样,对于每个环境,我们都有一个yml和属性文件。经过一年的开发,现在配置文件的单个yml文件变成了一个大型单片配置文件。 对于下面这样的配置文件,可以有多个配置文件吗?

  • 如何使用JavaScript同时设置多个属性?不幸的是,我不能在这个项目中使用像jQuery这样的框架。以下是我现在拥有的:

  • 简介 Apache ShardingSphere 提供属性配置的方式配置系统级配置。 配置项说明 名称 数据类型 说明 默认值 sql-show (?) boolean 是否在日志中打印 SQL。 打印 SQL 可以帮助开发者快速定位系统问题。日志内容包含:逻辑 SQL,真实 SQL 和 SQL 解析结果。 如果开启配置,日志将使用 Topic ShardingSphere-SQL,日志级别是 I