问题
I have created an instatiator bean for Dozer (http://dozer.sourceforge.net/). But when I Inject this EJB it throws nullpointer exception.
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();
}
}
It throws a null pointer exception. I have tried using it as a Stateless and Statefull bean but the result remains the same.
Any help would be appreciated. This is JAVA EE running on JBOSS AEP 6.
回答1:
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.
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");
}
回答2:
A singleton is included within Dozer API / this method should match your needs
Mapper getMapper() {
return DozerBeanMapperSingletonWrapper.getInstance();
}
If you want to turn it to CDI, see @Produce annotation specs.
来源:https://stackoverflow.com/questions/17596251/dozer-singleton-startup-bean-injetced-as-null