我正在按照this adviceEntityManager注入我的Jersey + HK2项目。由于某种原因,启动服务时突然看到此异常:


线程“主”中的异常java.lang.IllegalArgumentException:创建FactoryDe​​scriptor时必须将Factory作为第一个参数的协定
在org.glassfish.hk2.utilities.FactoryDe​​scriptorsImpl。(FactoryDe​​scriptorsImpl.java:78)
在org.glassfish.hk2.utilities.binding.AbstractBindingBuilder $ FactoryTypeBasedBindingBuilder.complete(AbstractBindingBuilder.java:453)
在org.glassfish.hk2.utilities.binding.AbstractBinder.resetBuilder(AbstractBinder.java:180)
在org.glassfish.hk2.utilities.binding.AbstractBinder.complete(AbstractBinder.java:190)
在org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:174)
在org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:187)
....


这是我的代码:

EMF工厂

public class EMFFactory implements Factory<EntityManagerFactory> {
    private final Logger log = LoggerFactory.getLogger(EMFFactory.class);
    protected EntityManagerFactory emf;

    @Inject
    Config config;

    @PostConstruct
    public void setup() {
        Properties p = new Properties();
        p.put("javax.persistence.jdbc.url", config.getJdbcUrl());
        p.put("javax.persistence.jdbc.user", config.getJdbcUser());
        p.put("javax.persistence.jdbc.password", config.getJdbcPassword());
        emf = Persistence.createEntityManagerFactory("skp-server-PU", p);
        log.debug("JDBC URL: "+ config.getJdbcUrl());
    }

    @Override
    public EntityManagerFactory provide() {
        return emf;
    }

    @Override
    public void dispose(EntityManagerFactory instance) {}

}


EM工厂

public class EMFactory implements Factory<EntityManager> {
    private final Logger log = LoggerFactory.getLogger(EMFFactory.class);
    private EntityManager em;

    @Inject
    EntityManagerFactory emf;

    @PostConstruct
    public void setup() {
        em = emf.createEntityManager();
        log.debug("New EntityManager created");
    }

    @Override
    public EntityManager provide() {
        return em;
    }

    @Override
    public void dispose(EntityManager instance) {
        log.debug("Disposing of EntityManager");
    }

}


ApplicationConfig绑定工厂:

    ServiceLocatorUtilities.bind(applicationLocator, new AbstractBinder() {

        @Override
        protected void configure() {
            bindFactory(EMFFactory.class)
                    .to(EntityManagerFactory.class)
                    .in(Singleton.class);
            bindFactory(EMFactory.class)
                    .to(EntityManager.class);
        }
    });


有人可以解释例外情况吗?

最佳答案

不知道它是否会帮助任何人,但是我发现我是如何打破它的:

我正在使用Maven Shade插件创建一个着色的uber-jar。该插件抱怨类重叠,因此我从阴影中排除了以下软件包:

<!-- This one comes with epcliselink, but I don't want shaded, hence the scope -->
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.1.0</version>
    <scope>provided</scope>
</dependency>


我的朋友们,那不是一个好主意。删除该部分解决了问题。

关于java - Jersey + HK2:EntityManager注入(inject)失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38901159/

10-10 09:48