我在TomEE中使用EJB 3.1,我想使用一个映射器,所以我选择了DOZER。
这是实现:

@Startup
@Singleton
public class DozerInstantiator {
private Logger log = Logger.getLogger(DozerInstantiator.class);

public enum States {BEFORESTARTED, STARTED, PAUSED, SHUTTINGDOWN};
private States state;
private DozerBeanMapper mapper;

  @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;
            }
        log.info("---------------Dozer  Started-----------------");   }

  public DozerBeanMapper getMapper() {
    return mapper;   }

}


这是我要注入Dozer的EJB:

@Stateless(name = "randomName")
@WebService(targetNamespace = "urn:x1:x2")
@Local
@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
public class RandomEJBImpl  implements RandomEJB {

@EJB
DozerInstantiator di;

        public ListOutput getAllConf() throws ServiceException {
            try {
                // code
                objMapped = di.getMapper().map(obj, X.class);
                // more code
                }
                return listOutput
            } catch (Exception e) {
                logger.error("Failed in RandomEJBImpl.getAllConf: "+e);
                return null;
            }
        }
 }


我有两个问题:

1-如果我尝试使用先前的DozerInstantiator配置启动服务器,则应用程序崩溃,如您在此处看到的,直到意识到如果删除@PostConstruct它将启动;你能告诉我为什么吗?

INFO  09-08 14:57:17,032 [main]: (DozerInstantiator.java:initialize:35)  ----------------Dozer  Starting Up-----------------
ERROR 09-08 14:57:17,093 [main]: (DozerInstantiator.java:initialize:41)  -Cause: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
ERROR 09-08 14:57:17,094 [main]: (DozerInstantiator.java:initialize:42)  -Message: org/apache/commons/lang3/StringUtils
ERROR 09-08 14:57:17,095 [main]: (DozerInstantiator.java:initialize:43)  -Class: class java.lang.NoClassDefFoundError
ERROR 09-08 14:57:17,095 [main]: (DozerInstantiator.java:initialize:44)  -StackTrace: [Ljava.lang.StackTraceElement;@7856e1fc


2-所以从DozerInstantiator删除@PostConstruct之后
我在日志文件中出现以下错误...如何避免呢?

ERROR 09-08 14:34:12,948 [http-8080-2]: (RandomEJBImpl.getAllConf:830)  -Failed in Failed in RandomEJBImpl.getAllConf: java.lang.NullPointerException

最佳答案

也许尝试在conf / system.properties中设置以下行:openejb.classloader.forced-load = org.apache.commons.lang3,以允许从Web应用程序加载lang3,并且不让我跳过Web应用程序以使用容器lang3版本

07-27 17:46