How to set @Autowired constructor params as “required=false” individually

If you’re using Java 8 and Spring Framework 4, you can use Optional.

@Autowired
public MyConfiguration(Optional<MyServiceA> myServiceA, Optional<MyServiceB> myServiceB){
  myServiceA.ifPresent(service->{this.myServiceA = service});
  myServiceB.ifPresent(service->{this.myServiceB = service});   
}

Leave a Comment