You can't really do it like that. Like Sotirios said, Spring needs to instantiate it to inject it into ClassA
. You probably can do it manually with the application context. Something like :
public class ClassA {
@Inject
private ApplicationContext appContext;
private ClassB classB;
//Bean will be instanciated when this method is called
public ClassB getClassB() {
if (classB == null) {
classB = appContext.getBean(ClassB.class);
}
return classB;
}
}
And then use the getter to access the object.