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

Damian Nowak commented on WICKET-1143:
--------------------------------------

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.

Reply via email to