Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

java - Replace Environment with @Value in Spring

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do as follows:

@Configuration
public class RabbitConfig {

    @Bean("googleRabbitService")
    public RabbitService getGoogleRabbitService(
                    @Value("rabbit-service.google.request-exchange-name") String requestExchangeName,
                    @Value("rabbit-service.google.request-queue-name") String requestQueueName,
                    @Value("rabbit-service.google.response-exchange-name") String responseExchangeName,
                    @Value("rabbit-service.google.dead-exchange-name") String deadExchangeName,
                    RabbitTemplate rabbitTemplate) {
        return new RabbitService(
                requestExchangeName,
                requestQueueName,
                responseExchangeName,
                deadExchangeName,
                rabbitTemplate
        );
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...