I see an error in the constructor which says
"String queueName" cannot be autowired.
I have AmazonSQSAsync
component defined in another class but not queueName
. Why is the constructor trying to autowire the parameters and how can I resolve this?
@Configuration
public class SqsQueueHealthIndicator extends AbstractHealthIndicator {
private final AmazonSQSAsync amazonSQSAsync;
private final String queueName;
public SqsQueueHealthIndicator(AmazonSQSAsync amazonSQSAsync, String queueName) {
this.amazonSQSAsync = amazonSQSAsync;
this.queueName = queueName;
}
@Override
protected void doHealthCheck(Health.Builder builder) {
try {
amazonSQSAsync.getQueueUrl(queueName);
builder.up();
} catch (QueueDoesNotExistException e) {
builder.down(e);
}
}
@Bean
SqsQueueHealthIndicator queueHealthIndicator(@Autowired AmazonSQSAsync amazonSQSAsync, @Value("${url}") String queueName) {
return new SqsQueueHealthIndicator(amazonSQSAsync, queueName);
}
@Bean
SqsQueueHealthIndicator deadLetterQueueHealthIndicator(@Autowired AmazonSQSAsync amazonSQSAsync, @Value("${dlqurl}") String deadLetterQueueName) {
return new SqsQueueHealthIndicator(amazonSQSAsync, deadLetterQueueName);
}
}
Because you're declaring it as final
so its value must be initialized.
You did not provide default value so Spring understand its value will be injected.
So you have 2 options:
final
modifier. Use @Value
to inject your value from config file. Create a bean type of String and inject it. You should name it:
@Bean("queueName") public String getQueueName {return "xyz";}
And inject like:
@Autowire
@Qualifier("queueName")
private final String queueName;
In normal circumstance, the option 1 is the go-to.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.