Spring在返回时不必要地修改Bean

时间:2016-11-21 07:57:47

标签: java spring spring-boot

我在Spring遇到了非常奇怪的行为。我有@Bean返回一张地图。但是,当Bean为@Autowired时,映射的键与@Bean方法中指定的键不同。我的@Bean有两个输入参数,也是来自另一个配置类的Spring Beans。一旦@Autowired我的地图的键被更改为与我的Map返回Bean中作为依赖项传入的@Bean方法的名称相匹配。有问题的@Bean位于@ConfigurationProperties类中,我从application.yml文件中提取一些值,这些值都正确返回。

@Component
@ConfigurationProperties(prefix = "channel-broker")
@EnableConfigurationProperties
public class ChannelLookupConfig {

    private String messageDeliveryChannelKey;

    private String otherDeliveryChannelKey;


    public String getMessageDeliveryChannelKey() {
        return messageDeliveryChannelKey;
    }

    public void setMessageDeliveryChannelKey(String messageDeliveryChannelKey) {
        this.messageDeliveryChannelKey = messageDeliveryChannelKey;
    }

    public String getOtherDeliveryChannelKey() {
        return otherDeliveryChannelKey;
    }

    public void setOtherDeliveryChannelKey(String OtherDeliveryChannelKey) {
        this.otherDeliveryChannelKey = OtherDeliveryChannelKey;
    }

    @Bean
    public Map<String, MessageDeliveryClient> channelCallerLookup(MessageDeliveryClient MessageDispatcherClient, MessageDeliveryClient otherDeliveryClient) {
        Map<String, MessageDeliveryClient> channelCallerLookup = new HashMap<>();
        channelCallerLookup.put(messageDeliveryChannelKey, MessageDispatcherClient);
        channelCallerLookup.put(otherDeliveryChannelKey, otherDeliveryClient);
        return channelCallerLookup;
    }
}

我的第二个配置文件

@Configuration
public class Config {
    @Bean
    public MessageDeliveryClient MessageDispatcherClient() {
        MessageDeliveryClient client = MessageDeliveryClient.builder()
                .awsAccessKey(destinationSqsAccessKey)
                .awsSecretKey(destinationSqsSecretKey)
                .awsRegion(destinationSqsRegion)
                .destinationQueueName(destinationSqsName)
                .build();
        return client;
    }

    @Bean
    public MessageDeliveryClient otherPickerDeliveryClient() {
        MessageDeliveryClient client = MessageDeliveryClient.builder()
                .awsAccessKey(destinationSqsAccessKey)
                .awsSecretKey(destinationSqsSecretKey)
                .awsRegion(destinationSqsRegion)
                .destinationQueueName(destinationOtherPickerSqsName)
                .build();
        return client;
    }
}

自动装配使用:

public class SimpleCustomerMessageDeliveryBrokerImpl implements CustomerMessageDeliveryBroker {
        private Map<String, MessageDeliveryClient> channelCallerLookup = new HashMap<>();

        @Autowired
        public void setBrokerConfiguration(BrokerConfiguration brokerConfiguration) {
            this.brokerConfiguration = brokerConfiguration;
        }
    }

Map应包含2个元素,第一个元素的键等于String messageDeliveryChannelKey中的值,第二个元素的键值等于String otherDeliveryChannelKey中的值。但是,键总是设置为等于传入我的分数的@Beans方法的名称。即使我将方法名称更改为无意义,地图的键也将等于该值。

如何防止此行为发生

1 个答案:

答案 0 :(得分:0)

这是因为默认的Spring行为。为了解决这个问题,我在返回Map周围应用了一个Wrapper。

将我的Bean更改为此

@Bean
public ChannelCallerLookup channelCallerLookup(MessageDeliveryClient messageDispatcherClient, MessageDeliveryClient otherPickerDeliveryClient) {
    HashMap<String, MessageDeliveryClient> channelCallerLookup = new HashMap<>();
    channelCallerLookup.put(CHANNEL1_KEY, messageDispatcherClient);
    channelCallerLookup.put(CHANNEL1_KEY2, otherPickerDeliveryClient);
    ChannelCallerLookup callerLookup = new ChannelCallerLookup(channelCallerLookup);
    return callerLookup;
}

创建此包装类

public class ChannelCallerLookup {

    Map<String, MessageDeliveryClient> lookupMap;

    public ChannelCallerLookup(Map<String, MessageDeliveryClient> lookupMap) {
        this.lookupMap = lookupMap;
    }

    public Map<String, MessageDeliveryClient> getLookupMap() {
        return lookupMap;
    }

    public MessageDeliveryClient get(String key){
        return lookupMap.get(key);
    }
}