سؤال

having this generic DAO definition

@Repository
public class GenericService<T> implements IGenericService<T> {
    @PersistenceContext(unitName="mgrUnit", name="mgrEMF")
    @Qualifier(value = "mgrEMF")
    public void setEm(EntityManager em) {       
        this.em = em;       
        util = em.getEntityManagerFactory().getPersistenceUnitUtil();       
    }
}

and having a large number of Entities, i want to automatiqualy instanciate DAO, Back-End Beans for basic tables like ( Region, EmployeSpeciality... )

For the bean registration and instantion it's easy but what about the DAO ? I must precise that in my case the EntityManager depends on the service, i have a multiple Database connection.

i read this article but honnestly it looks to much complicated

http://doanduyhai.wordpress.com/2011/11/20/spring-transactional-explained/

Is there a simple way to do it ?

OK, using

AutowireCapableBeanFactory beanFactory = 
FacesUtils.getWebappContext().getAutowireCapableBeanFactory();
beanFactory.autowireBean(obj);

It solve the half of the problem, the Bean EMF is correctly injected, but the bean is not registred (i must re-instanciate it every time i need), because the beanFactory doesnt contains the Bean Definition, so how to add it?

NB: i know that puting the object in the application MAP can keep the DAO accessible but its not a serious solution

FacesContext.getCurrentInstance().getExternalContext().
            getApplicationMap().put( serviceName, service);
هل كانت مفيدة؟

المحلول

IGenericService service = (IGenericService) ContextManager.findBean( serviceName );     
if (service==null && !FacesUtils.getWebappContext().getBeanFactory().
           containsBeanDefinition(serviceName)){

   service = new ErpGenericService(clazz);
   AutowireCapableBeanFactory beanFactory = FacesUtils.getWebappContext().
                                             getAutowireCapableBeanFactory();
   beanFactory.autowireBean(service);
   //Applying transactional proxies
   service = (IGenericService) beanFactory.applyBeanPostProcessorsAfterInitialization(service, serviceName);
   FacesUtils.getWebappContext().getBeanFactory().
                     registerSingleton(serviceName, service);                       

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top