mardi 2 juin 2015

spring chaining constructor with service injection

I having a class, like this

private TestService testService;

public testClass() {
    this(testService);
}

@Autowired(required = true)
public testClass(TestService testService) {
    this.testService = testService;
}

where the default no-argument constructor is mandatory, 'cause I having a structured factory design, from there I amm calling my no-argument constructor so I don't want to changes this structure. So I went with chaining constructor to call argumented constructor from default constructor. But java is throwing the exception

cannot reference testService before supertype constructor has been called,

I can solve this exception by having testService as static, but from here, I red that having static injection is not always a good idea.

Can some one advice me with some design solution on how can solve or call this argumented constructor from default constructor without having static injection.

Aucun commentaire:

Enregistrer un commentaire