|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.picocontainer.gems.adapters.DelegateMethodAdapter<T>
public class DelegateMethodAdapter<T>
Object construction is sometimes expensive, especially when it is seldom used
object. The goal of this adapter is to use the
DelegateMethod
type to allow delayed
construction of objects.
For example, in a web application, to be able to have classes that depend on the HttpSession object, you would have to call HttpServletRequest.getSession(true). This is fine unless you don't want to create a session until you absolutely have to.
Enter DelegateMethodAdapter:
//Assumed Variables: request == HttpServletRequest. //Typical PicoContainer MutablePicoContainer pico = new PicoBuilder().withLifecycle().withCaching() .build(); //Create a delegate method that will invoke HttpServletRequest.getSession(true) when invoke() is called. DelegateMethod delegateMethod = new DelegateMethod(HttpServletRequest.class, "getSession", true); //Create the Adapter wrapping the delegate method. DelegateMethodAdapter methodAdapter = new DelegateMethodAdapter( HttpSession.class, request, delegateMethod); pico.addAdapter(methodAdapter); //If only executing this code, the HttpSession should not be created yet. assertNull(request.getSession(false)); //Will get the session object by having the delegate method call HttpServletRequest.getSession(true) HttpSession session = pico.getComponent(HttpSession.class); assertNotNull(session); //Should demonstrate that the session has now been created. assertNotNull(request.getSession(false));
With an adapter like this, you can write classes like:
public class SessionUser { public SessionUser(HttpSession session) { //..... } }
With impunity, and are guaranteed that the session would not be created unless the SessionUser object was constructed.
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface org.picocontainer.ComponentAdapter |
---|
ComponentAdapter.NOTHING |
Constructor Summary | |
---|---|
DelegateMethodAdapter(Object componentKey,
ComponentMonitor monitor,
Object targetInstance,
DelegateMethod factoryMethod)
|
|
DelegateMethodAdapter(Object componentKey,
Object targetInstance,
DelegateMethod factoryMethod)
|
Method Summary | |
---|---|
void |
accept(PicoVisitor visitor)
Accepts a visitor for this ComponentAdapter. |
ComponentAdapter<T> |
findAdapterOfType(Class componentAdapterType)
Locates a component adapter of type componentAdapterType in the ComponentAdapter chain. |
Class<T> |
getComponentImplementation()
Retrieve the class of the component. |
T |
getComponentInstance(PicoContainer container)
Deprecated. |
T |
getComponentInstance(PicoContainer container,
Type into)
Returns the |
Object |
getComponentKey()
Retrieve the key associated with the component. |
ComponentAdapter<T> |
getDelegate()
No delegates. |
String |
getDescriptor()
Get a string key descriptor of the component adapter. |
void |
verify(PicoContainer container)
Verify that all dependencies for this adapter can be satisfied. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DelegateMethodAdapter(Object componentKey, Object targetInstance, DelegateMethod factoryMethod)
componentKey
- Component
- Implementation will be the expected return type of the factory
method.public DelegateMethodAdapter(Object componentKey, ComponentMonitor monitor, Object targetInstance, DelegateMethod factoryMethod)
componentKey
- componentImplementation
- monitor
- Method Detail |
---|
public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException
getComponentInstance
in interface ComponentAdapter<T>
container
- the PicoContainer
, that is used to resolve any possible dependencies of the instance.into
- the class that is about to be injected into. Use ComponentAdapter.NOTHING.class if this is not important to you.
PicoCompositionException
- if the component has dependencies which could not be resolved, or
instantiation of the component lead to an ambiguous situation within the
container.public String getDescriptor()
getDescriptor
in interface ComponentAdapter<T>
ComponentAdapter.getDescriptor()
public void verify(PicoContainer container) throws PicoCompositionException
verify
in interface ComponentAdapter<T>
container
- the PicoContainer
, that is used to resolve any possible dependencies of the instance.
PicoCompositionException
- if one or more dependencies cannot be resolved.public void accept(PicoVisitor visitor)
PicoContainer
, that
cascades the visitor also down to all its ComponentAdapter instances.
accept
in interface ComponentAdapter<T>
visitor
- the visitor.public ComponentAdapter<T> findAdapterOfType(Class componentAdapterType)
findAdapterOfType
in interface ComponentAdapter<T>
componentAdapterType
- the class of the adapter type being located. Never null.
public Class<T> getComponentImplementation()
getComponentImplementation
in interface ComponentAdapter<T>
@Deprecated public T getComponentInstance(PicoContainer container) throws PicoCompositionException
Cached
will always return the
same instance.
getComponentInstance
in interface ComponentAdapter<T>
container
- the PicoContainer
, that is used to resolve any possible dependencies of the instance.
PicoCompositionException
- if the component has dependencies which could not be resolved, or
instantiation of the component lead to an ambigous situation within the
container.public Object getComponentKey()
getComponentKey
in interface ComponentAdapter<T>
public ComponentAdapter<T> getDelegate()
getDelegate
in interface ComponentAdapter<T>
|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |