Now that we've seen how useful other service beans can be in our Java code, let's make it easier to get to those services. The best way to get an instance of the runtime bean, or any other project bean, is to "inject" it into the service class using dependency injection.
Dependency injection is when the bean declares a dependency and the container (for example, Tomcat and Spring) provides or injects that dependency to the bean as part of its initialization. The benefit is that the property is populated for us by the container. We do not need to fetch an instance of the service in code.
In our example, DbOpsSvc
from the JavaServices
project, we also inject runtimeService
into our service class. The updateCustomerInjected()
method repeats the updateCustomer()
example function but using dependency injection.
The runtime service has the bean ID of runtimeService
. We can confirm the bean ID for any bean by examining that service's spring.xml
file. In the case of the runtime...