org.wicketstuff.springreference
Class SpringReference<T>

java.lang.Object
  extended by org.wicketstuff.springreference.AbstractSpringReference<T>
      extended by org.wicketstuff.springreference.SpringReference<T>
Type Parameters:
T - type of the wrapped spring bean
All Implemented Interfaces:
Serializable, Cloneable, IClusterable

public class SpringReference<T>
extends AbstractSpringReference<T>
implements IClusterable

This class together with SpringReferenceSupporter is an alternative to wicket-spring's @SpringBean and SpringComponentInjector to integrate spring with wicket in a web application.

Inspired by the concept of Reference classes and by the implementation of @SpringBean this class was made to overcome the shortcomings of dynamic proxying classes (need for no-private no-arg constructor, final methods not working in some cases, method annotations lost). If you used @SpringBean and ever saw mysterious stack traces like:

 
 Caused by: java.lang.IllegalArgumentException: Protected method: fooMethod()V
        at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:196)
        at org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:319)
 
 
or
 
 Caused by: java.lang.IllegalArgumentException: No visible constructors in class FooClass
        at net.sf.cglib.proxy.Enhancer.filterConstructors(Enhancer.java:531)
        at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:448)
        at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
        at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
        at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
        at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
 
 
This class is the solution.

Because this class does not need an IComponentInstantiationListener, does not use dynamic proxies and looks up spring beans lazily, it should be slightly faster. It also supports serializing as @SpringBean does.

Instances of this class use the SpringReferenceSupporter for bean lookup, so it must be registered in your wicket WebApplication init() method ( SpringReferenceSupporter.register(this);). Otherwise you will get NullPointerException when calling AbstractSpringReference.get(). See SpringReferenceSupporter for more information.

Declaration (you can declare it in your wicket components, models or your custom classes as well):

 
 private final SpringReference<AuthenticationManager> authenticationManagerRef = SpringReference.of(AuthenticationManager.class, "authenticationManager");
 private final SpringReference<AbstractRememberMeServices> rememberMeServicesRef = SpringReference.of(AbstractRememberMeServices.class);
 
 
Access:
 
 authenticationManagerRef.get().authenticate(token);
 AbstractRememberMeServices rememberMeServices = rememberMeServicesRef.get();
 
 

Author:
akiraly
See Also:
Serialized Form

Constructor Summary
SpringReference(Class<T> clazz, String name)
          Constructor.
 
Method Summary
 SpringReference<T> clone()
           
protected  SpringReferenceSupporter getSupporter()
           
static
<T> SpringReference<T>
of(Class<T> clazz)
          Creator method for easy usage.
static
<T> SpringReference<T>
of(Class<T> clazz, String name)
          Creator method for easy usage.
 
Methods inherited from class org.wicketstuff.springreference.AbstractSpringReference
equals, get, getClazz, getName, hashCode, isClazzBasedOnlyLookup, setInstanceRef, setName
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpringReference

public SpringReference(Class<T> clazz,
                       String name)
Constructor.

Parameters:
clazz - class of the wrapped spring bean, not null
name - beanName of the wrapped spring bean, can be null
Method Detail

of

public static <T> SpringReference<T> of(Class<T> clazz)
Creator method for easy usage. Use this if you only want to specify the type of the spring bean.

Type Parameters:
T - type of the wrapped spring bean
Parameters:
clazz - class of the wrapped spring bean, not null
Returns:
new SpringReference instance wrapping the spring bean

of

public static <T> SpringReference<T> of(Class<T> clazz,
                                        String name)
Creator method for easy usage. Use this if need to specify a spring bean by name too.

Type Parameters:
T - type of the wrapped spring bean
Parameters:
clazz - class of the wrapped spring bean, not null
name - name of the wrapped spring bean, can be null
Returns:
new SpringReference instance wrapping the spring bean

getSupporter

protected SpringReferenceSupporter getSupporter()
Specified by:
getSupporter in class AbstractSpringReference<T>
Returns:
object used to find the wrapped spring bean, not null

clone

public SpringReference<T> clone()
Overrides:
clone in class AbstractSpringReference<T>


Copyright © 2012. All Rights Reserved.