Skip to content

SpringReference

akiraly edited this page Apr 4, 2011 · 6 revisions

SpringReference Project

About

The wicketstuff-springreference module can be used to integrate a wicket web application with spring. It is independent from wicket-spring and can be used for cases where @SpringBean is not an option.

Why?

From wicket-spring the @SpringBean annotation together with the SpringComponentInjector class give a slick way to inject spring beans into components. However it is using a technology called "dynamic proxy" and this is not always working. This limits the usability of @SpringBean in some cases. When you hit these limitations you probably see strange exceptions like these:

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:320)

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)

The SpringReference class from wicketstuff-springreference is not annotation/dynamic proxy based so it works in all cases (but using it does not look that cool as an annotation). Because SpringReference does not depend on IComponentInstantiationListener-s and looks up beans lazily it could also be slightly faster than @SpringBean.

Maven snippet

Add this to your pom.xml:

<dependency>
	<groupId>org.wicketstuff</groupId>
	<artifactId>wicketstuff-springreference</artifactId>
	<version>[some-version]</version>
</dependency>

Java snippet

To enable SpringReference support you have to register a SpringReferenceSupporter with your WebApplication. This can be done in the init() method:

public class App extends WebApplication {
	@Override
	public Class<? extends Page> getHomePage() {
		return HomePage.class;
	}

	@Override
	protected void init() {
		super.init();
		getMarkupSettings().setDefaultMarkupEncoding(CharEncoding.UTF_8);

		/* --> */ SpringReferenceSupporter.register(this); /* <-- */
	}
}

Now you can refer spring beans in your wicket components, models, etc. and in your custom classes too:

// reference by type and bean id
private final SpringReference<AuthenticationManager> authenticationManagerRef =
					SpringReference.of(AuthenticationManager.class, "authenticationManager");
// reference by type only
private final SpringReference<AbstractRememberMeServices> rememberMeServicesRef = 
					SpringReference.of(AbstractRememberMeServices.class);

And to access the beans you can use the get() method of the SpringReference objects:

authenticationManagerRef.get().authenticate(token);
AbstractRememberMeServices rememberMeServices = rememberMeServicesRef.get();

Dependencies

Wicketstuff-springreference depends on wicket-core 1.5-RC3<= and spring-web 3.0<=.

Further reading

For more info see the javadoc of SpringReference and SpringReferenceSupporter classes. For a complete @SpringBean - SpringReference example see the "wicketstuff-springreference-examples" web application.

Clone this wiki locally