All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wicketstuff.springreference.SpringReference Maven / Gradle / Ivy

Go to download

This module can be used in cases when limited by @SpringBean. It can be used to complete or replace wicket-spring. See SpringReference class for more info.

The newest version!
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wicketstuff.springreference;

import java.lang.ref.Reference;

import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.util.io.IClusterable;

/**
 * 

* This class together with {@link 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 {@link 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 {@link SpringReferenceSupporter} for bean lookup, so it must * be registered in your wicket {@link WebApplication} init() method ( * SpringReferenceSupporter.register(this);). Otherwise you will get * {@link NullPointerException} when calling {@link #get()}. See {@link 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();
 * 
 * 
* *

* * @param * type of the wrapped spring bean * * @author akiraly */ public class SpringReference extends AbstractSpringReference implements IClusterable { private static final long serialVersionUID = -1798985501215878818L; /** * Constructor. * * @param clazz * class of the wrapped spring bean, not null * @param name * beanName of the wrapped spring bean, can be null */ public SpringReference(Class clazz, String name) { super(clazz, name); } /** * Creator method for easy usage. Use this if you only want to specify the type of the spring * bean. * * @param * type of the wrapped spring bean * @param clazz * class of the wrapped spring bean, not null * @return new SpringReference instance wrapping the spring bean */ public static SpringReference of(Class clazz) { return of(clazz, null); } /** * Creator method for easy usage. Use this if need to specify a spring bean by name too. * * @param * type of the wrapped spring bean * @param clazz * class of the wrapped spring bean, not null * @param name * name of the wrapped spring bean, can be null * @return new SpringReference instance wrapping the spring bean */ public static SpringReference of(Class clazz, String name) { return new SpringReference(clazz, name); } @Override protected SpringReferenceSupporter getSupporter() { return SpringReferenceSupporter.get(); } @Override public SpringReference clone() { return (SpringReference)super.clone(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy