T - type of the wrapped spring beanpublic class SpringReference<T> extends AbstractSpringReference<T> implements org.apache.wicket.util.io.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();
| Constructor and Description |
|---|
SpringReference(Class<T> clazz,
String name)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
equals, get, getClazz, getName, hashCode, isClazzBasedOnlyLookup, setInstanceRef, setNamepublic static <T> SpringReference<T> of(Class<T> clazz)
T - type of the wrapped spring beanclazz - class of the wrapped spring bean, not nullpublic static <T> SpringReference<T> of(Class<T> clazz, String name)
T - type of the wrapped spring beanclazz - class of the wrapped spring bean, not nullname - name of the wrapped spring bean, can be nullprotected SpringReferenceSupporter getSupporter()
getSupporter in class AbstractSpringReference<T>public SpringReference<T> clone()
clone in class AbstractSpringReference<T>Copyright © 2015. All rights reserved.