Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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 spring boot@Autowired中的多个配置属性其中一个属性为空_Java_Spring Boot_Amazon Sqs - Fatal编程技术网

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

Java spring boot@Autowired中的多个配置属性其中一个属性为空,java,spring-boot,amazon-sqs,Java,Spring Boot,Amazon Sqs,我是spring新手,我有以下引导应用程序类。我正在尝试从Spring boot应用程序连接到AWS SQS。代码如下: @SpringBootApplication @EnableConfigurationProperties ({ApplicationProperties.class, AwsProperties.class}) public class Application{ private static final Logger logger = LoggerFactory.g

我是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: 创建在类路径中定义了名为“createSQSClient”的bean时出错 resource[io/bigbear/midb/sqs/JmsConfig.class]:通过 工厂方法失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:未能 实例化[com.amazonaws.services.sqs.AmazonSQSClient]:工厂 方法“createSQSClient”引发异常;嵌套异常是 java.lang.IllegalArgumentException:访问密钥不能为null


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

密钥在错误消息中:
访问密钥不能为null
。看起来您需要(至少)修复
AwsProperties.getAccessKey()
,这样它就不会返回null。