Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

injecting spring bean in java 8 function

This is a how I do it as well as a should I do it question.

I have a case where I need to use instances managed by Spring in a Lambda function. In short I want DummyBranch, MandrillBranch objects and other branches to be managed by Spring.

public class QOSStrategy {

  public static Function<DistributionMessage, List<Feedback>> executeQOS = (message)-> {
    QOSFilters qosFilters = new QOSFilters();
    List<Branch> providers = qosFilters.getProviderByQOs(message, 
    Arrays.asList(new DummyEmailBranch(), 
        new MandrillBranch(), 
        new EverbrideBranch(), 
        new JavaMailBranch(), 
        new DirectSMSBranch()));
  }
}

One option I see is to get it from the Spring application-context. But is there a way to do it using Annotations.

Thanks -Parshu

like image 666
user3869813 Avatar asked Feb 27 '26 08:02

user3869813


1 Answers

You can autowire Collection of all beans which implement the same interface.

See Spring documentation here.

like image 124
Artem Avatar answered Mar 02 '26 07:03

Artem