【发布时间】:2017-01-23 19:54:49
【问题描述】:
我遇到的错误与在注入到 Test 类之前解析属性有关。注入属性时,我最终得到${property.name}。但是,考虑到存在嵌套依赖关系,Test 类的配置似乎非常错误。
具体错误:Caused by: java.net.URISyntaxException: Illegal character in authority at index 8: https://${sqs.endpoint}
我有一个配置类来为 @Bean 加载特定的道具:
@Configuration
public class AWSConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(AWSConfig.class);
private @Value("${sqs.endpoint}") String endpoint;
@Bean(name = "awsClient")
@Primary
public AmazonSQSAsyncClient amazonSQSClient() {
AmazonSQSAsyncClient awsSQSAsyncClient
= new AmazonSQSAsyncClient();
awsSQSAsyncClient.setEndpoint(endpoint);
return awsSQSAsyncClient;
}
}
这是@Bean 的注入位置:
@Component
public class SqsQueueSender {
private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSender.class);
private final QueueMessagingTemplate queueMessagingTemplate;
@Autowired
@Qualifier("awsClient")
AmazonSQSAsyncClient amazonSQSAsyncClient;
public SqsQueueSender(AmazonSQSAsync amazonSQSAsyncClient) {
this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSQSAsyncClient);
}
//take advantage of convertAndSend to send POJOs in appropriate format
public void send(String queueName, String message) {
this.queueMessagingTemplate.convertAndSend(queueName, MessageBuilder.withPayload(message).build());
}
}
这一切似乎都有效,至少应用程序启动并从任一位置打印日志。不过,我无法针对此代码运行单元测试。我不知道如何正确设置配置。这是测试类的最新迭代:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class SqsQueueSenderTest {
@Configuration
static class ContextConfiguration {
private @Value("${sqs.endpoint}") String endpoint;
@Bean(name = "awsClient")
@Primary
public AmazonSQSAsyncClient amazonSQSClient() {
AmazonSQSAsyncClient awsSQSAsyncClient
= new AmazonSQSAsyncClient();
awsSQSAsyncClient.setEndpoint(endpoint);
return awsSQSAsyncClient;
}
@Bean
public SqsQueueSender sqsQueueSender() {
SqsQueueSender sqsQueueSender = new SqsQueueSender(amazonSQSClient());
// set up the client
return sqsQueueSender;
}
}
@Autowired
SqsQueueSender sqsQueueSender;// = new SqsQueueSender(new AmazonSQSAsyncClient());
private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSenderTest.class);
// attributes for in-memory sqs server
AmazonSQSClient client;
SQSRestServer server;
SQSRestServerBuilder sqsRestServerBuilder;
@Before
public void startup() {
LOGGER.info("Building in-memory SQS server");
this.server = sqsRestServerBuilder.withPort(9324).withInterface("localhost").start();
this.client = new AmazonSQSClient(new BasicAWSCredentials("x", "x"));
client.setEndpoint("http://localhost:9324");
client.createQueue("test");
LOGGER.info("Finished building in-memory SQS server");
}
@After
public void shutdown() {
LOGGER.info("Stopping in-memory SQS server");
server.stopAndWait();
LOGGER.info("Finished stopping in-memory SQS server");
}
@Test
public void testSending() {
LOGGER.info("~~~~~~~~~~~~~");
sqsQueueSender.send("test", "new message");
LOGGER.info("The current queues are" + client.listQueues().toString());
LOGGER.info("~~~~~~~~~~~~~");
}
}
【问题讨论】:
标签: java spring spring-boot dependency-injection configuration