I’ve got the following class:
@Service
public class TestService{
@Autowired
private Integer size;
private MyClass myObject;
public Test(){
// Default constructor for Spring
}
public Test(MyClass myObject){
this.myObject = myObject;
}
// Getters and Setters ...
}
Now I’m instantiating my TestService from outside this class with
new TestService(myObject);
The variable size is of course null. But I want it to be injected from the spring context even though I didn’t inject the whole object.
Is it possible to always inject some fields?
The easiest way would be to use static AspectJ compilation (or load time weaving) and
@Configurable: any Object with the@Configurableannotation will be autowired, even when instantiated throughnew()But the more traditional way would be to wire it up programatically: