生产者参数注入?

mem*_*und 0 java jpa dependency-properties java-ee cdi

如何创建一个根据参数创建对象的生成器方法?

我的目标是能够CrudService在我的应用程序中注入不同的类,但是使用服务所用的类(例如User.class)来对其进行参数化.

以下代码当然不起作用,但说明了我的意图.

@Produces
@JPAContainer(Class type) //something like this?
public JPAContainer getJPA() {
    @PersistenceContext
    private EntityManager em;

    @Produces
    @JPAContainerAnnot
    public JPAContainer getJPAContainer() {
       return JPAContainerFactory.make(type, em); //eg: class = User.class, Person.class
    }
}

@Stateless
public class CrudServiceUser() {
   @Inject
   @JPAContainer(type = User.class) //something like this parameter
   private JPAContainer container;
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD,METHOD,PARAMETER,TYPE})
public @interface JPAContainer {

}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ent 5

是的,您可以将InjectionPoint对象注入生产者方法.您的限定符的type属性需要@Nonbinding.从InjectionPoint您可以参考注入点上的注释,找到您的JPAContainer并从中读取值.