How can i replace Environment
with @Value
?
I have a config and want to replace Environment
and getProperty
:
@Configuration
public class RabbitConfig {
@Autowired
Environment environment;
@Bean("googleRabbitService")
public RabbitService getGoogleRabbitService(RabbitTemplate rabbitTemplate) {
return new RabbitService(
environment.getProperty("rabbit-service.google.request-exchange-name"),
environment.getProperty("rabbit-service.google.request-queue-name"),
environment.getProperty("rabbit-service.google.response-exchange-name"),
environment.getProperty("rabbit-service.google.dead-exchange-name"),
rabbitTemplate
);
}
@Bean("amazonRabbitService")
public RabbitService getAmazonRabbitService(RabbitTemplate rabbitTemplate) {
return new RabbitService(
environment.getProperty("rabbit-service.amazon.request-exchange-name"),
environment.getProperty("rabbit-service.amazon.request-queue-name"),
environment.getProperty("rabbit-service.amazon.response-exchange-name"),
environment.getProperty("rabbit-service.amazon.dead-exchange-name"),
rabbitTemplate
);
}
With @Value("${rabbit-service.google.request-exchange-name}")
straight in the constructor I get the error: Annotations are not allowed here
.
I could use fields:
@Value("${rabbit-service.gogle.request-exchange-name}")
private String googleRequestExchange;
@Value("${rabbit-service.gogle.request-exchange-name}")
private String googleResponseExchange;
...
And then:
public RabbitService getGoogleRabbitService(RabbitTemplate rabbitTemplate) {
return new RabbitService(
googleRequestExchange,
googleResponseExchange,
...
rabbitTemplate
);
But I will have to create a bunch of fields. Is there any way to use @Value
?
question from:
https://stackoverflow.com/questions/65915218/replace-environment-with-value-in-spring 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…