Jersey + HK2:EntityManager 注入失败
Jersey + HK2: EntityManager injection fails
我正在关注 以将 EntityManager
注入到我的 Jersey + HK2 项目中。不知为何,启动服务时突然看到这个异常:
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)
....
这是我的代码:
EMFFactory
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
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 阴影插件创建一个阴影 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>
我的朋友们,这不是个好主意。删除部分解决了问题。
我正在关注 EntityManager
注入到我的 Jersey + HK2 项目中。不知为何,启动服务时突然看到这个异常:
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) ....
这是我的代码:
EMFFactory
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
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 阴影插件创建一个阴影 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>
我的朋友们,这不是个好主意。删除部分解决了问题。