2014-12-12 2 views
0

Wildfly 8.2.0에 배포 된 Java EE 응용 프로그램을 구현 중입니다. 필드 이름으로 삽입하고자하는 EntityManager가 여러 개 있습니다. 이를 위해 InjectionPoint에서 필드 이름을 가져 오는 @Produces를 사용하여 생성자 메서드를 만들었습니다. 이 솔루션은 지금까지 작동CDI 필드 이름으로 EntityManagers를 @Produces로 삽입

@Inject 
private EntityManager primaryEm; 

,하지만 필드에 의해 주입 "이를 구현하기 CDI의 또 다른, 더 우아한 방법이 있습니다 :

public class Resources { 

    @PersistenceContext(unitName = "primary") 
    private EntityManager primaryEm; 

    @PersistenceContext(unitName = "secondary") 
    private EntityManager secondaryEm; 

    @Produces 
    public EntityManager getEntityManager(InjectionPoint injectionPoint) 
     throws Exception { 
     Field field = getClass().getDeclaredField(
       injectionPoint.getMember().getName()); 

     return (EntityManager) field.get(this); 
    } 
} 

그럼 난 단순히 적절한 필드 이름으로 EntityManager를 삽입 할 수 이름 "기능?

답변

1

가능한 한 가장 좋은 솔루션이라고 생각합니다. 또는 당신은 당신에게 우아함이 무엇을 의미하는지 자세히 설명 할 수 있습니까?

+0

나는 그것을 읽은 것을 기쁘게 생각합니다. 글쎄, 나는 반사를 포함하여 그 생산자 방법없이이 문제를 해결할 가능성이 있는지 궁금해했다. –

관련 문제