简体   繁体   English

泽西岛+ HK2:EntityManager注入失败

[英]Jersey + HK2: EntityManager injection fails

I'm following this advice to get an EntityManager injected into my Jersey + HK2 project. 我正在按照此建议EntityManager注入到我的Jersey + HK2项目中。 For some reason, I suddenly see this exception when starting the service: 由于某种原因,启动服务时突然看到此异常:

Exception in thread "main" java.lang.IllegalArgumentException: Creation of FactoryDescriptors must have Factory as a contract of the first argument at org.glassfish.hk2.utilities.FactoryDescriptorsImpl.(FactoryDescriptorsImpl.java:78) at org.glassfish.hk2.utilities.binding.AbstractBindingBuilder$FactoryTypeBasedBindingBuilder.complete(AbstractBindingBuilder.java:453) at org.glassfish.hk2.utilities.binding.AbstractBinder.resetBuilder(AbstractBinder.java:180) at org.glassfish.hk2.utilities.binding.AbstractBinder.complete(AbstractBinder.java:190) at org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:174) at org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:187) .... 线程“主”中的异常java.lang.IllegalArgumentException:创建FactoryDe​​scriptors必须具有Factory作为org.glassfish.hk2.utilities.FactoryDe​​scriptorsImpl。(FactoryDe​​scriptorsImpl.java:78)上第一个参数的协定。 utilities.binding.AbstractBindingBuilder $ FactoryTypeBasedBindingBuilder.complete(AbstractBindingBuilder.java:453)在org.glassfish.hk2.utilities.binding.AbstractBinder.resetBuilder(AbstractBinder.java:180)在org.glassfish.hk2.utilities.binding.AbstractBinder。 org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:187)处的Complete(AbstractBinder.java:190)在org.glassfish.hk2.utilities.binding.AbstractBinder.bind(AbstractBinder.java:174) ..

Here's my code: 这是我的代码:

EMFFactory 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) {}

}

EMFactory 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");
    }

}

The ApplicationConfig binds the factories: 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);
        }
    });

Can someone explain the exception? 有人可以解释例外情况吗?

Not sure if it will ever help anyone, but I found out how I broke it: 不知道它是否会帮助任何人,但是我发现我是如何打破它的:

I am creating a shaded uber-jar with the maven shade plugin. 我正在使用Maven Shade插件创建一个着色的uber-jar。 The plugin was complaining about overlapping classes, so I excluded the following package from being shaded: 该插件抱怨类重叠,因此我从阴影中排除了以下软件包:

<!-- 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>

That, my friends, was not a good idea. 我的朋友们,那不是一个好主意。 Removing the section fixed the problem. 删除该部分解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM