I'm experiencing really strange behavior from Spring. I have a @Bean
which returns a Map. However when the Bean is @Autowired
in, the key for the map is different than what was assigned in the @Bean
method. my @Bean
has two input parameters which are also Spring Beans from another configuration class. Once @Autowired
The Keys of my map are changed to match the name of the @Bean
methods passed in as dependencies im my Map returning Bean. The @Bean
in question is located in an @ConfigurationProperties
class where I am extracting some values from my application.yml file which are all returning correctly.
@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;
}
}
My second config file
@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;
}
}
Autowired in for use as such:
public class SimpleCustomerMessageDeliveryBrokerImpl implements CustomerMessageDeliveryBroker {
private Map<String, MessageDeliveryClient> channelCallerLookup = new HashMap<>();
@Autowired
public void setBrokerConfiguration(BrokerConfiguration brokerConfiguration) {
this.brokerConfiguration = brokerConfiguration;
}
}
the Map should contain 2 elements the first with a key equal to the value in String messageDeliveryChannelKey
and the second with a key equal to the value in String otherDeliveryChannelKey
. However the keys are always set equal to the name of the @Beans methods which are passed into my score. Even if I change the method names to nonsense the map's keys will equal that value.
How can I prevent this behavior from happening
This was occurring because of default Spring behavior. To work around this I applied a Wrapper around the return Map.
Changed my Bean to this
@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;
}
Created This Wrapper Class
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);
}
}
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.