public class SpringHelper
extends java.lang.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.
SpringBean
Constructor and Description |
---|
SpringHelper() |
Modifier and Type | Method and Description |
---|---|
protected static java.lang.Object |
findSpringBean(org.springframework.context.ApplicationContext ctx,
java.lang.String name,
java.lang.Class<?> type,
boolean allowFindByType)
Looks up a Spring managed bean from an Application Context.
|
protected static java.util.Collection<java.lang.reflect.Field> |
getFields(java.lang.Class<?> clazz)
Fetches the fields on a class that are annotated with SpringBean.
|
protected static java.util.Collection<java.lang.reflect.Method> |
getMethods(java.lang.Class<?> clazz)
Fetches the methods on a class that are annotated with SpringBean.
|
static void |
injectBeans(java.lang.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(java.lang.Object bean,
org.springframework.context.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(java.lang.Object bean,
javax.servlet.ServletContext ctx)
Injects Spring managed beans using a Web Application Context derived from
the ServletContext.
|
protected static java.lang.String |
methodToPropertyName(java.lang.reflect.Method m)
A slightly unusual, and somewhat "loose" conversion of a method name to a property
name.
|
public static void injectBeans(java.lang.Object bean, ActionBeanContext context)
bean
- the object into which to inject spring managed beancontext
- the ActionBeanContext represented by the current requestpublic static void injectBeans(java.lang.Object bean, javax.servlet.ServletContext ctx)
bean
- the object to have beans injected intoctx
- the ServletContext to use to find the Spring ApplicationContextpublic static void injectBeans(java.lang.Object bean, org.springframework.context.ApplicationContext ctx)
@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.bean
- the bean into which to inject spring beansctx
- the Spring application contextprotected static java.util.Collection<java.lang.reflect.Method> getMethods(java.lang.Class<?> clazz)
clazz
- the class on which to look for SpringBean annotated methodsprotected static java.util.Collection<java.lang.reflect.Field> getFields(java.lang.Class<?> clazz)
clazz
- the class on which to look for SpringBean annotated fieldsprotected static java.lang.Object findSpringBean(org.springframework.context.ApplicationContext ctx, java.lang.String name, java.lang.Class<?> type, boolean allowFindByType)
ctx
- the Spring Application Contextname
- the name of the spring bean to look fortype
- the type of bean to look forallowFindByType
- true to indicate that finding a bean by type is acceptable
if find by name fails.java.lang.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.protected static java.lang.String methodToPropertyName(java.lang.reflect.Method m)
setFoo
to foo
conversion if the method follows the normal
syntax, otherwise will just return the method name.m
- the method to determine the property name of? Copyright 2005-2006, Stripes Development Team.