You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by "Damian Nowak (JIRA)" <ji...@apache.org> on 2010/09/26 20:08:32 UTC

[jira] Issue Comment Edited: (WICKET-1143) Modify InjectorHolder to allow for storage of multiple types of Injectors (for Guice support)

    [ https://issues.apache.org/jira/browse/WICKET-1143?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12915011#action_12915011 ] 

Damian Nowak edited comment on WICKET-1143 at 9/26/10 2:06 PM:
---------------------------------------------------------------

I managed to create some almost-working Injector for both Java EE and Spring. EJBs and Spring Beans get injected with this.

Almost-working because I get serialization exceptions - I don't know why Wicket needs the class serializable as the classes from Java EE injector and Spring injector are not and they work.

If anyone is able to correct it so that no serialization exceptions are thrown, feel free.

in Application.init():

{noformat}
addComponentInstantiationListener(new SpringComponentInjector(this));
addComponentInstantiationListener(new JavaEEComponentInjector(this, createNamingStrategy()));
final SpringAndJavaEeInjector springAndJavaEeInjector = new SpringAndJavaEeInjector(this, new StandardJndiNamingStrategy()); // I use my own naming strategy, StandardJndiNamingStrategy is the default
InjectorHolder.setInjector(springAndJavaEeInjector);
{noformat}

{noformat}
import java.io.Serializable;
import java.lang.reflect.Field;
import org.apache.wicket.IClusterable;
import org.apache.wicket.injection.ConfigurableInjector;
import org.apache.wicket.injection.IFieldValueFactory;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.ISpringContextLocator;
import org.apache.wicket.spring.injection.annot.AnnotProxyFieldValueFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.wicketstuff.javaee.injection.JavaEEProxyFieldValueFactory;
import org.wicketstuff.javaee.naming.IJndiNamingStrategy;

public class SpringAndJavaEeInjector extends ConfigurableInjector implements Serializable {

	private final SpringAndJavaEeFieldValueFactory factory;

	public SpringAndJavaEeInjector(final WebApplication webApplication, final IJndiNamingStrategy namingStrategy) {
		JavaEEProxyFieldValueFactory javaEeFactory = new JavaEEProxyFieldValueFactory(namingStrategy);
		AnnotProxyFieldValueFactory springFactory = new AnnotProxyFieldValueFactory(new ISpringContextLocator() {

			@Override
			public ApplicationContext getSpringContext() {
				return WebApplicationContextUtils.getRequiredWebApplicationContext(webApplication.getServletContext());
			}
		}, true);
		factory = new SpringAndJavaEeFieldValueFactory(javaEeFactory, springFactory);
	}

	@Override
	protected IFieldValueFactory getFieldValueFactory() {
		return factory;
	}

	private static class SpringAndJavaEeFieldValueFactory implements IFieldValueFactory {

		private final JavaEEProxyFieldValueFactory javaEeFactory;

		private final AnnotProxyFieldValueFactory springFactory;

		public SpringAndJavaEeFieldValueFactory(JavaEEProxyFieldValueFactory javaEeFactory, AnnotProxyFieldValueFactory springFactory) {
			this.javaEeFactory = javaEeFactory;
			this.springFactory = springFactory;
		}

		@Override
		public Object getFieldValue(Field field, Object fieldOwner) {
			try {
				final Object fieldValue = javaEeFactory.getFieldValue(field, fieldOwner);
				if (fieldValue == null) {
					throw new RuntimeException();
				}
				return fieldValue;
			} catch (Exception e) {
				return springFactory.getFieldValue(field, fieldOwner);
			}
		}

		@Override
		public boolean supportsField(Field field) {
			final boolean supportsField = javaEeFactory.supportsField(field);
			if (supportsField) {
				return true;
			}
			return springFactory.supportsField(field);
		}
	}

}
{noformat}

Ehh, Wiki syntax is turned off here. Here is a formatted and highlighted code: http://wklej.org/id/393829/

      was (Author: nowaker):
    I managed to create some almost-working Injector for both Java EE and Spring. EJBs and Spring Beans get injected with this.

Almost-working because I get serialization exceptions - I don't know why Wicket needs the class serializable as the classes from Java EE injector and Spring injector are not and they work.

in Application.init():

{noformat}
addComponentInstantiationListener(new SpringComponentInjector(this));
addComponentInstantiationListener(new JavaEEComponentInjector(this, createNamingStrategy()));
final SpringAndJavaEeInjector springAndJavaEeInjector = new SpringAndJavaEeInjector(this, new StandardJndiNamingStrategy()); // I use my own naming strategy, StandardJndiNamingStrategy is the default
InjectorHolder.setInjector(springAndJavaEeInjector);
{noformat}

{noformat}
import java.io.Serializable;
import java.lang.reflect.Field;
import org.apache.wicket.IClusterable;
import org.apache.wicket.injection.ConfigurableInjector;
import org.apache.wicket.injection.IFieldValueFactory;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.ISpringContextLocator;
import org.apache.wicket.spring.injection.annot.AnnotProxyFieldValueFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.wicketstuff.javaee.injection.JavaEEProxyFieldValueFactory;
import org.wicketstuff.javaee.naming.IJndiNamingStrategy;

public class SpringAndJavaEeInjector extends ConfigurableInjector implements Serializable {

	private final SpringAndJavaEeFieldValueFactory factory;

	public SpringAndJavaEeInjector(final WebApplication webApplication, final IJndiNamingStrategy namingStrategy) {
		JavaEEProxyFieldValueFactory javaEeFactory = new JavaEEProxyFieldValueFactory(namingStrategy);
		AnnotProxyFieldValueFactory springFactory = new AnnotProxyFieldValueFactory(new ISpringContextLocator() {

			@Override
			public ApplicationContext getSpringContext() {
				return WebApplicationContextUtils.getRequiredWebApplicationContext(webApplication.getServletContext());
			}
		}, true);
		factory = new SpringAndJavaEeFieldValueFactory(javaEeFactory, springFactory);
	}

	@Override
	protected IFieldValueFactory getFieldValueFactory() {
		return factory;
	}

	private static class SpringAndJavaEeFieldValueFactory implements IFieldValueFactory {

		private final JavaEEProxyFieldValueFactory javaEeFactory;

		private final AnnotProxyFieldValueFactory springFactory;

		public SpringAndJavaEeFieldValueFactory(JavaEEProxyFieldValueFactory javaEeFactory, AnnotProxyFieldValueFactory springFactory) {
			this.javaEeFactory = javaEeFactory;
			this.springFactory = springFactory;
		}

		@Override
		public Object getFieldValue(Field field, Object fieldOwner) {
			try {
				final Object fieldValue = javaEeFactory.getFieldValue(field, fieldOwner);
				if (fieldValue == null) {
					throw new RuntimeException();
				}
				return fieldValue;
			} catch (Exception e) {
				return springFactory.getFieldValue(field, fieldOwner);
			}
		}

		@Override
		public boolean supportsField(Field field) {
			final boolean supportsField = javaEeFactory.supportsField(field);
			if (supportsField) {
				return true;
			}
			return springFactory.supportsField(field);
		}
	}

}
{noformat}

If anyone is able to correct it so that no serialization exceptions are thrown, feel free.
  
> Modify InjectorHolder to allow for storage of multiple types of Injectors (for Guice support)
> ---------------------------------------------------------------------------------------------
>
>                 Key: WICKET-1143
>                 URL: https://issues.apache.org/jira/browse/WICKET-1143
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket, wicket-guice
>    Affects Versions: 1.3.0-beta4
>            Reporter: Peter Mularien
>            Assignee: Martin Grigorov
>
> As per discussion on the mailing list:
> http://www.nabble.com/Question-about-Guice-integration-with-Wicket-1.3-beta-4-tf4778901.html#a13672025
> InjectorHolder should play nicely with GuiceComponentInjector, and allow for mixed systems using both Spring and Guice. Thanks!

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.