public <T> T createContextualInstance(Contextual<T> bean, CreationalContext<T> creationalContext)
    {
        Object beanKey = getBeanKey(bean);
        if (isConcurrent())
        {
            // locked approach
            ContextualInstanceInfo<T> instanceInfo = new ContextualInstanceInfo<T>();

            ConcurrentMap<Object, ContextualInstanceInfo<?>> concurrentMap
                = (ConcurrentHashMap<Object, ContextualInstanceInfo<?>>) contextualInstances;

            ContextualInstanceInfo<T> oldInstanceInfo
                = (ContextualInstanceInfo<T>) concurrentMap.putIfAbsent(beanKey, instanceInfo);

            if (oldInstanceInfo != null)
            {
                instanceInfo = oldInstanceInfo;
            }
            synchronized (instanceInfo)
            {
                T instance = instanceInfo.getContextualInstance();
                if (instance == null)
                {
                    instance = bean.create(creationalContext);
                    instanceInfo.setContextualInstance(instance);
                    instanceInfo.setCreationalContext(creationalContext);
                }

                return instance;
            }

        }
        else
        {
            // simply create the contextual instance
            ContextualInstanceInfo<T> instanceInfo = new ContextualInstanceInfo<T>();
            instanceInfo.setCreationalContext(creationalContext);
            instanceInfo.setContextualInstance(bean.create(creationalContext));

            contextualInstances.put(beanKey, instanceInfo);

            return instanceInfo.getContextualInstance();
        }
    }
public < T > T createContextualInstance ( Contextual < T > bean , CreationalContext < T > creationalContext ) { Object beanKey = getBeanKey ( bean ) ; if ( isConcurrent ( ) ) { ContextualInstanceInfo < T > instanceInfo = new ContextualInstanceInfo < T > ( ) ; ConcurrentMap < Object , ContextualInstanceInfo < ? > > concurrentMap = ( ConcurrentHashMap < Object , ContextualInstanceInfo < ? > > ) contextualInstances ; ContextualInstanceInfo < T > oldInstanceInfo = ( ContextualInstanceInfo < T > ) concurrentMap . putIfAbsent ( beanKey , instanceInfo ) ; if ( oldInstanceInfo != null ) { instanceInfo = oldInstanceInfo ; } synchronized ( instanceInfo ) { T instance = instanceInfo . getContextualInstance ( ) ; if ( instance == null ) { instance = bean . create ( creationalContext ) ; instanceInfo . setContextualInstance ( instance ) ; instanceInfo . setCreationalContext ( creationalContext ) ; } return instance ; } } else { ContextualInstanceInfo < T > instanceInfo = new ContextualInstanceInfo < T > ( ) ; instanceInfo . setCreationalContext ( creationalContext ) ; instanceInfo . setContextualInstance ( bean . create ( creationalContext ) ) ; contextualInstances . put ( beanKey , instanceInfo ) ; return instanceInfo . getContextualInstance ( ) ; } }
@param bean
@param creationalContext
@param <T>
@return