net.sourceforge.stripes.integration.spring
Class SpringHelper

java.lang.Object
  extended by net.sourceforge.stripes.integration.spring.SpringHelper

public class SpringHelper
extends Object

Static helper class that is used to lookup Spring beans and inject them into objects (often ActionBeans). Is capable of injecting beans through setter methods (property access) and also through direct field access if the security policy allows it. Methods and fields must be annotated using the @SpringBean annotation.

Methods and fields may be public, protected, package-access or private. If they are not public an attempt is made to call AccessibleObject.setAccessible(boolean) in order to make them accessible from this class. If the attempt fails, an exception will be thrown.

Method names can take any form. For example setSomeBean(Bean b) or someBean(bean b). In both cases, if a specific SpringBean name is not supplied, the default name of someBean will be used.

The value of the @SpringBean annotation should be the bean name in the Spring application context if it is different from the field/property name. If the value is left blank, an attempt is made to auto-wire the bean; first by field/property name and then by type. If the value is left blank and more than one bean of the same type is found, an exception will be raised.

The first time that any of the injection methods in this class is called with a specific type of object, the object's class is examined for annotated fields and methods. The discovered fields and methods are then cached for future usage.

Author:
Dan Hayes, Tim Fennell
See Also:
SpringBean

Constructor Summary
SpringHelper()
           
 
Method Summary
protected static Object findSpringBean(ApplicationContext ctx, String name, Class<?> type, boolean allowFindByType)
          Looks up a Spring managed bean from an Application Context.
protected static Collection<Field> getFields(Class<?> clazz)
          Fetches the fields on a class that are annotated with SpringBean.
protected static Collection<Method> getMethods(Class<?> clazz)
          Fetches the methods on a class that are annotated with SpringBean.
static void injectBeans(Object bean, ActionBeanContext context)
          Injects Spring managed beans into using a Web Application Context that is derived from the ServletContext, which is in turn looked up using the ActionBeanContext.
static void injectBeans(Object bean, ApplicationContext ctx)
          Looks for all methods and fields annotated with @SpringBean and attempts to lookup and inject a managed bean into the field/property.
static void injectBeans(Object bean, ServletContext ctx)
          Injects Spring managed beans using a Web Application Context derived from the ServletContext.
protected static String methodToPropertyName(Method m)
          A slightly unusual, and somewhat "loose" conversion of a method name to a property name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringHelper

public SpringHelper()
Method Detail

injectBeans

public static void injectBeans(Object bean,
                               ActionBeanContext context)
Injects Spring managed beans into using a Web Application Context that is derived from the ServletContext, which is in turn looked up using the ActionBeanContext.

Parameters:
bean - the object into which to inject spring managed bean
context - the ActionBeanContext represented by the current request

injectBeans

public static void injectBeans(Object bean,
                               ServletContext ctx)
Injects Spring managed beans using a Web Application Context derived from the ServletContext.

Parameters:
bean - the object to have beans injected into
ctx - the ServletContext to use to find the Spring ApplicationContext

injectBeans

public static void injectBeans(Object bean,
                               ApplicationContext ctx)
Looks for all methods and fields annotated with @SpringBean and attempts to lookup and inject a managed bean into the field/property. If any annotated element cannot be injected an exception is thrown.

Parameters:
bean - the bean into which to inject spring beans
ctx - the Spring application context

getMethods

protected static Collection<Method> getMethods(Class<?> clazz)
Fetches the methods on a class that are annotated with SpringBean. The first time it is called for a particular class it will introspect the class and cache the results. All non-overridden methods are examined, including protected and private methods. If a method is not public an attempt it made to make it accessible - if it fails it is removed from the collection and an error is logged.

Parameters:
clazz - the class on which to look for SpringBean annotated methods
Returns:
the collection of methods with the annotation

getFields

protected static Collection<Field> getFields(Class<?> clazz)
Fetches the fields on a class that are annotated with SpringBean. The first time it is called for a particular class it will introspect the class and cache the results. All non-overridden fields are examined, including protected and private fields. If a field is not public an attempt it made to make it accessible - if it fails it is removed from the collection and an error is logged.

Parameters:
clazz - the class on which to look for SpringBean annotated fields
Returns:
the collection of methods with the annotation

findSpringBean

protected static Object findSpringBean(ApplicationContext ctx,
                                       String name,
                                       Class<?> type,
                                       boolean allowFindByType)
Looks up a Spring managed bean from an Application Context. First looks for a bean with name specified. If no such bean exists, looks for a bean by type. If there is only one bean of the appropriate type, it is returned. If zero or more than one bean of the correct type exists, an exception is thrown.

Parameters:
ctx - the Spring Application Context
name - the name of the spring bean to look for
type - the type of bean to look for
allowFindByType - true to indicate that finding a bean by type is acceptable if find by name fails.
Throws:
RuntimeException - various subclasses of RuntimeException are thrown if it is not possible to find a unique matching bean in the spring context given the constraints supplied.

methodToPropertyName

protected static String methodToPropertyName(Method m)
A slightly unusual, and somewhat "loose" conversion of a method name to a property name. Assumes that the name is in fact a mutator for a property and will do the usual setFoo to foo conversion if the method follows the normal syntax, otherwise will just return the method name.

Parameters:
m - the method to determine the property name of
Returns:
a String property name


? Copyright 2005-2006, Stripes Development Team.