No, you cannot refer to the entered fields in the constructor. The structure must somehow construct your object (call the constructor), and then introduce the dependencies so that they are empty at the time the constructor runs. Instead, you use annotation @PostConstruct to one of your methods and do it initialize:
class MyBean { @Inject private MyDependency myDep; @PostConstruct public void init() { assert myDep != null; } }
In the case of spring xml configuration, you can use init-method="init" instead of @PostConstruct in the <bean> definition. Alternatively, you can use constructor injection in xml:
<bean id="myBean" class="my.package.MyBean"> <constructor-arg ref="myDependency/> </bean>
or equivalent annotation.
source share