Caching

Caching is where a container, if asked for a component a second time, gives the same instance. If you had design relying on Singletons and wanted to move to the DI age, this concept for you. Once interface/impl separated, with public static methods, and under caching Pico control, they become managed single instances.

CachingPicoContainer

This is a convenience class that implements all of the functionality of DefaultPicoContainer, but with the guaranteed caching of instances of components:

An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/caching/BasicCachingExampleTestCase.java. The system administrator has been notified.

It does not matter if you choose another ComponentAdapterFactory (CAF), the combination of CachingPicoContainer and that CAF will still cache:

An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/caching/BasicCachingExampleTestCase.java. The system administrator has been notified.

DefaultPicoContainer and CachingComponentAdapterFactory

DefaultPicoContainer (DPC) also caches by default:

An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/caching/BasicCachingExampleTestCase.java. The system administrator has been notified.

But if passed a specific ComponentAdapterFactory that does not cache, no caching will occur:

An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/caching/BasicCachingExampleTestCase.java. The system administrator has been notified.

Caching with DPC can be done the hard way:

An error occurred: http://svn.codehaus.org/picocontainer/java/picocontainer/trunk/container/src/test/org/picocontainer/doc/caching/BasicCachingExampleTestCase.java. The system administrator has been notified.

This introduced the secret of CachingPicoContainer. The CachingComponentAdapterFactory that is first is a chain of CAFs. For CachingPicoContainer is is secretly added to be first in the chain, for the snippet above for DPC, it was explicitly added. By this mechanism, any CAF for DPC can be instructed to cache. The default CAF for DPC to use when one is not explicitly injected, is DefaultComponentAdapterFactory which caches via CachingComponentAdapterFactory, and otherwise uses ConstructorInjectionComponentAdapters for components.

Labels

 
(None)