Skip to content

06. Adding explicit instances

Jim Riordan edited this page Aug 10, 2016 · 1 revision

###Adding explicit instances to the container

So far we've been adding objects to the container by specifying classes we want to be injected and relying on a dependency resolution mechanism. In some situations, we want to explicitly tell the container to instantiate an object using a new operator or invoke a static factory method ourselves. For those purposes, Yadic provides an addInstance method.

container.addInstance(ScheduledExecutorService.class, Executors.newScheduledThreadPool(5)); 

In the example above, we're explicitly telling Yadic to create ScheduledExecutorService using a static factory method (newScheduledThreadPool).

Clone this wiki locally