dimanche 20 août 2017

Builder Pattern With Spring

Say you have a spring component like below with dependencies constructor injected (builder is a singleton):

@Component
public class houseBuilder{

     private WindowMaker windowMaker;
     private DoorMaker doorMaker;
     private RoofMaker roofMaker;

     @Autowired
     public houseBuilder(WindowsMaker wm, DoorMaker dm, RoofMaker rm){
            this.windowMaker = wm;
            this.doorMaker = dm;
            this.roofMaker = rm;
     }

     //Other methods omitted for brevity

}

However the house requires something called "foundation" that should be passed in through the constructor or set before making the house begins and foundation is not a spring bean. I'd like to use the builder pattern as well so I could do something like the below but I am unsure how to do this with spring. Something like the below is what I am after except I want to use spring:

Foundation foundation = new Foundation();

HouseBuilder hb = new HouseBuilder(foundation)
.windowMaker(args)
.doorMaker(args)
.roofMaker(args);

Any advice is appreciated.

Aucun commentaire:

Enregistrer un commentaire