org.wicketstuff.springreference
Class AbstractSpringReferenceSupporter

java.lang.Object
  extended by org.wicketstuff.springreference.AbstractSpringReferenceSupporter
Direct Known Subclasses:
SpringReferenceSupporter

public abstract class AbstractSpringReferenceSupporter
extends Object

Abstract base class for spring bean finding. Used by AbstractSpringReference.

This class is intended to be used in a singleton/service way because it maintains caches to speed up lookups. Subclasses must implement the getApplicationContext() method used to locate the spring context. This class does not depend on wicket or spring-web. So in theory subclasses can be used in non-wicket, non-web spring applications too.

Author:
akiraly

Constructor Summary
AbstractSpringReferenceSupporter()
           
 
Method Summary
 void clearCache()
          Clears the internal cache.
<T> T
findAndSetInstance(AbstractSpringReference<T> ref)
          Looks up the spring bean from a spring ApplicationContext.
protected
<T> String
findBeanName(AbstractSpringReference<T> ref)
          Finds out the exact name for a spring bean.
protected abstract  ApplicationContext getApplicationContext()
           
protected  BeanDefinition getBeanDefinition(ConfigurableListableBeanFactory fact, String name)
          Tries to get the BeanDefinition of a spring bean.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractSpringReferenceSupporter

public AbstractSpringReferenceSupporter()
Method Detail

findAndSetInstance

public <T> T findAndSetInstance(AbstractSpringReference<T> ref)
Looks up the spring bean from a spring ApplicationContext. The bean is set in the reference. If the name was not given for the reference this method will fill that too. Throws a RuntimeException if the bean could not be found.

Type Parameters:
T - type of the wrapped spring bean
Parameters:
ref - reference where the instance will be set
Returns:
loaded spring bean or throws a RuntimeException if loading failed.

findBeanName

protected <T> String findBeanName(AbstractSpringReference<T> ref)
Finds out the exact name for a spring bean. Used when only the class was given by the user. If there is not exactly one candidate (even after taking primary beans into account) an exception is thrown.

Type Parameters:
T - type of the wrapped spring bean
Parameters:
ref - reference to find name for
Returns:
name of the spring bean or IllegalStateException if there is not exactly one candidate.

getBeanDefinition

protected BeanDefinition getBeanDefinition(ConfigurableListableBeanFactory fact,
                                           String name)
Tries to get the BeanDefinition of a spring bean.

Parameters:
fact - spring bean factory
name - spring bean name to find definition for
Returns:
bean definition, can be null

getApplicationContext

protected abstract ApplicationContext getApplicationContext()
Returns:
context where the spring bean is searched for, not null

clearCache

public void clearCache()
Clears the internal cache.



Copyright © 2012. All Rights Reserved.