我是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时出错在类路径中定义 resource [io / bigbear / midb / sqs / JmsConfig.class]:Bean实例化通过 工厂方法失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化[com.amazonaws.services.sqs.AmazonSQSClient]:工厂 方法' createSQSClient'抛出异常;嵌套异常是 java.lang.IllegalArgumentException:访问键不能为空。
答案 0 :(得分:0)
我不确定,但似乎你的<input type="text" class="input">
<button class="btn">Click me</button>
<ul class="cont"></ul>
<script>
function createHtml(str) {
return `<li>${str}</li>`;
}
function insertHtml(document, contClassName, str) {
document.querySelector(`.${contClassName}`).innerHTML += str;
}
function handler() {
insertHtml(document, 'cont', createHtml(document.querySelector(`.input`).value));
}
document.querySelector('.btn').addEventListener('click', handler);
</script>
会返回null。