问题描述
我为Dozer创建了一个instatiator bean()。但是当我注入这个EJB时,它会抛出nullpointer异常。
I have created an instatiator bean for Dozer (http://dozer.sourceforge.net/). But when I Inject this EJB it throws nullpointer exception.
这是我用于初始化推土机的Singleton启动Bean
Here is my Singleton Startup Bean for intialising Dozer
package com.unijunction.ordercloud.common.rest;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Singleton;
import javax.ejb.Startup;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Startup
@Singleton
public class DozerInstantiator {
private DozerBeanMapper mapper;
private Logger log = LoggerFactory.getLogger(DozerInstantiator.class);
public enum States {
BEFORESTARTED, STARTED, PAUSED, SHUTTINGDOWN
};
private States state;
//TODO: DozerIsntantiator is retunring null
@PostConstruct
public void initialize() throws Throwable {
log.info("---------------Dozer Starting Up-----------------");
try{
state = States.BEFORESTARTED;
this.mapper = new DozerBeanMapper();
} catch (Throwable e) {
log.error("Cause: " + e.getCause());
log.error("Message: " + e.getMessage());
log.error("Class: " + e.getClass());
log.error("StackTrace: " + e.getStackTrace());
throw e;
}
state = States.STARTED;
log.info("---------------Dozer Started-----------------");
}
@PreDestroy
public void terminate() {
state = States.SHUTTINGDOWN;
// Perform termination
log.info("---------------Dozer Shuttingdown-----------------");
}
public States getState() {
return this.state;
}
public void setState(States state) {
this.state = state;
}
public DozerBeanMapper getMapper() {
return this.mapper;
}
}
它注入的类是泛型类看起来像这样:
The class it is being injected into is a generic class and looks like this:
public class DtoConverter<T1, T> {
protected final Class<T> entityClass;
@EJB
DozerInstantiator dozerInstantiator;
//instance for dozer to convert beans
protected DozerBeanMapper mapper;
private Logger log = LoggerFactory.getLogger(DozerInstantiator.class);
public DtoConverter(){
this.entityClass = null;
}
/**
* Setup the class
*
*/
public DtoConverter(Class<T> entityClass) {
this.entityClass = entityClass;
//TODO: only allow one instance of this to exist
//https://ordercloud.atlassian.net/browse/API-80
try{
mapper = dozerInstantiator.getMapper(); //<--- This throws null pointer
}catch(Exception e){
mapper = new DozerBeanMapper();
}
}
它抛出空指针异常。我已经尝试将它用作Stateless和Statefull bean,但结果保持不变。
It throws a null pointer exception. I have tried using it as a Stateless and Statefull bean but the result remains the same.
任何帮助都将不胜感激。这是在JBOSS AEP 6上运行的JAVA EE。
Any help would be appreciated. This is JAVA EE running on JBOSS AEP 6.
推荐答案
事实证明CDI规范不允许注入单例进入泛型类。所以我写了一个静态最终的dozer实例的延迟初始化。
It turns out that the CDI spec does not allow a singleton to be injected into a generic type class. So I wrote a lazy initialization of a static final dozer instance.
我在DozerInstantiator中添加了以下两个方法:
I added the following two methods to the DozerInstantiator:
public static DozerBeanMapper getInstance(){
return MapperHolder.instance;
}
private static class MapperHolder{
static final DozerBeanMapper instance = new DozerBeanMapper(mappingList);
}
然后在泛型类中调用它:
It is then called in the generic type class :
DozerInstantiator dozerInstantiator = new DozerInstantiator();
try{
mapper = dozerInstantiator.getInstance();
}catch(Exception e){
log.error("Failed to initialise singleton Dozer bean");
}
这篇关于Dozer Singleton Startup Bean被称为Null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!