我为 Dozer ( http://dozer.sourceforge.net/ ) 创建了一个 instatiator bean。但是当我注入这个 EJB 时,它会抛出空指针异常。
这是我用于初始化推土机的单例启动 Bean
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;
}
}
它被注入的类是一个泛型类,如下所示:
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();
}
}
它抛出一个空指针异常。我曾尝试将它用作无状态和有状态 bean,但结果保持不变。
任何帮助,将不胜感激。这是在 JBOSS AEP 6 上运行的 JAVA EE。