Getting NullPointerException while using onEjbCrea

2019-09-08 15:41发布

问题:

I have an existing application developed using Spring 2.5, which I had to migrate to 3.2.6. After the migration, everything is working fine.. except I'm getting a NullPointerException while using onEjbCreate() method of Deprecated AbstractStatelessSessionBean in Spring 3.2.6 . I think the problem is that onEjbCreate() is not compatible with EJB 3.0. I tried using @PostConstruct ,but then I was not able to get what to substitute for the existing getBeanFactory().

Would appreciate if anyone can help me with this. Thanks.

This is the existing code that was working on Spring 2.5

@Override
protected void onEjbCreate() throws CreateException {
    mqConnectorFactory = (ConnectorFactory) getBeanFactory().getBean(BEAN_NAME_MQ_CONN_FACTORY);
}

回答1:

As you noticed AbstractStatelessSessionBean is deprecated in favor of EJB3 style implementation and in Spring 4.0 it is removed altogether.

For EJB3 the Spring provides SpringBeanAutowiringInterceptor

Having that you can simply using this:

@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MyEjb {

   @Autowired
   private ConnectionFactory mqConnectorFactory;

}

Of course, you should be sure that you configure Spring correctly: beanRefContext.xml in the classpath.