srisris
srisris

Reputation: 569

How to correctly configure list, map and set in Spring Java Based Configuration

How to achieve this in Spring based configuration.

<util:list id="abcHandlersList">
            <ref bean="aHandler" />
            <ref bean="bHandler" />
            <ref bean="cHandler" />
    </util:list>

The scenario I want to achieve. I have a different paymentHandlers e.g. Paypal, CreditCard and DebitCard. In my Service I will get a list of handlers and iterate to find an appropriate handler to process my payment. I would really appreciate if you could help me with Java Based Spring configuration.

Thanks in advance

Upvotes: 1

Views: 2744

Answers (1)

Avinash Singh
Avinash Singh

Reputation: 3797

I haven't seen any annotations to support list , maps etc in Java Based configuration

You need to create your list and maps in your Configuration class like ,

@Bean
  public List<String> abcHandlersList(){
      List<String> abcHandlers = new ArrayList<>();
      abcHandlers.add(AHandler1());
      abcHandlers.add(BHandler1());
      return abcHandlers ;
 }

  @Bean
    public AHandler AHandler1(){
            return new AHandler();
    }

  @Bean
    public BHandler BHandler1(){
            return new BHandler();
    }

You can add the list to bean , other way around like

@Resource(name="abcHandlersList") 
  public void setHandlers(List<String> handlers) {
      this.handlers = handlers;
  }

or

@Resource(name="abcHandlersList")
private List<String> handlers;

Upvotes: 3

Related Questions