2 Replies Latest reply on Dec 28, 2009 12:22 PM by Nicklas Karlsson

    EEFields connot inject into proxy

    he youlin Novice
      
      I have a bean. It have ee field 
      @PersistenceContext
      EntityManager entityManager;
      
      And i have a interceptor. When interceptor do aroundInvoke context's target have null entityManager
      
      I edit org.jboss.weld.injection.FieldInjectionPoint.java
      
      public void inject(Object declaringInstance, Object value)
         {
            try
            {
               delegate().set(declaringInstance, value);
            }
            catch (IllegalArgumentException e)
            {
               rethrowException(e);
            }
            catch (IllegalAccessException e)
            {
               rethrowException(e);
            }
         }
      
      to
      
      
      public void inject(Object declaringInstance, Object value)
         {
              try
                 {
                    Object instanceToInject = declaringInstance;
                    if (!isDelegate())
                    {
                       // if declaringInstance is a proxy, unwrap it
                       instanceToInject = InterceptionUtils.getRawInstance(declaringInstance);
                    }
                    delegate().set(instanceToInject,value);
                 }
            catch (IllegalArgumentException e)
            {
               rethrowException(e);
            }
            catch (IllegalAccessException e)
            {
               rethrowException(e);
            }
         }
      
      
      It works.
      
      
      I user tomcat6.1.20  and JpaInjectionServices show below:
      
      public class SpringJpaInjectionService implements JpaInjectionServices{
           private WebApplicationContext webApplicationContext;
           public SpringJpaInjectionService(ServletContext servletContext) {
                webApplicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
           }
      
           @Override
           public EntityManager resolvePersistenceContext(InjectionPoint injectionPoint) {
                if (!injectionPoint.getAnnotated().isAnnotationPresent(PersistenceContext.class))
                {
                     throw new IllegalArgumentException("No @PersistenceContext annotation found on injection point " + injectionPoint);
                }
                String unitname=injectionPoint.getAnnotated().getAnnotation(PersistenceContext.class).unitName();
                Map<String, AbstractEntityManagerFactoryBean> emfbeans=webApplicationContext.getBeansOfType(AbstractEntityManagerFactoryBean.class, true, true);
                for(Entry<String, AbstractEntityManagerFactoryBean> entry:emfbeans.entrySet())
                {
                     AbstractEntityManagerFactoryBean absenfBean=entry.getValue();
                     if(unitname==null || unitname.equals(""))
                          return ExtendedEntityManagerCreator.createContainerManagedEntityManager(absenfBean.getNativeEntityManagerFactory());
                     else if(absenfBean.getPersistenceUnitName()!=null && absenfBean.getPersistenceUnitName().equals(unitname))
                          return ExtendedEntityManagerCreator.createContainerManagedEntityManager(absenfBean.getNativeEntityManagerFactory());
                     
                }
                return null;
           }
      
           @Override
           public EntityManagerFactory resolvePersistenceUnit(InjectionPoint injectionPoint) {
                if (!injectionPoint.getAnnotated().isAnnotationPresent(PersistenceUnit.class))
                {
                     throw new IllegalArgumentException("No @PersistenceContext annotation found on injection point " + injectionPoint);
                }
                String unitname=injectionPoint.getAnnotated().getAnnotation(PersistenceUnit.class).unitName();
                Map<String, AbstractEntityManagerFactoryBean> emfbeans=webApplicationContext.getBeansOfType(AbstractEntityManagerFactoryBean.class, true, true);
                for(Entry<String, AbstractEntityManagerFactoryBean> entry:emfbeans.entrySet())
                {
                     AbstractEntityManagerFactoryBean absenfBean=entry.getValue();
                     if(unitname==null || unitname.equals(""))
                          return absenfBean.getNativeEntityManagerFactory();
                     else if(absenfBean.getPersistenceUnitName()!=null && absenfBean.getPersistenceUnitName().equals(unitname))
                          return absenfBean.getNativeEntityManagerFactory();
                     
                }
                return null;
           }
      
           @Override
           public void cleanup() {
                // TODO Auto-generated method stub
                
           }
      
      }