簡體   English   中英

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

[英]Spring unnecessarily modifying Bean on Return

我從Spring經歷了非常奇怪的行為。 我有一個@Bean返回一個Map。 但是,當Bean使用@Autowired插入時,映射的鍵與@Bean方法中分配的鍵不同。 我的@Bean有兩個輸入參數,它們也是另一個配置類中的Spring Bean。 一旦@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;
        }
    }

地圖應該包含2種元素的第一與密鑰等於在字符串的值messageDeliveryChannelKey和第二用鑰匙等於在字符串的值otherDeliveryChannelKey 但是,鍵始終設置為等於傳遞到我的樂譜中的@Beans方法的名稱。 即使我將方法名稱更改為廢話,地圖的鍵也將等於該值。

如何防止這種行為發生

發生這種情況是由於默認的Spring行為。 要解決此問題,我在返回Map周圍應用了包裝器。

將我的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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM