Sachin
Sachin

Reputation: 17

Inject a bean as a constructor argument in a child class

How to inject a bean as a constructor argument in a child class when the parent class constructor takes a list of that type.

@Service
public class Parent{
    private List<MyObject> myObjectList;
    public Parent(List<MyObject> myObjectList){
        this.myObjectList = myObjectList;
    }
}

@Service
public class Child extends Parent {
    @Autowired
    public Child(MyObject myObject){
        super( ???? );
    }
}

Upvotes: 0

Views: 148

Answers (1)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

What about this ?

super(Arrays.asList(new MyObject[] {myObject}));

Upvotes: 4

Related Questions