我是 Spring 新手,我有以下引导应用程序类。我正在尝试从Spring启动应用程序连接到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时出错
资源[io / bigbear / midb / sqs / JmsConfig.class]:通过Bean实例化
工厂方法失败;嵌套异常为
org.springframework.beans.BeanInstantiationException:失败
实例化[com.amazonaws.services.sqs.AmazonSQSClient]:工厂
方法'createSQSClient'抛出异常;嵌套异常为
java.lang.IllegalArgumentException:访问密钥不能为null。
最佳答案
我不确定,但看来您的awsProperties.getAccessKey()
返回null。