Picture the following scenario: You have an Enterprise Application Archive (EAR) which contains an EJB module and a WAR file. The web application uses a Spring application context and the same application context must be – for some reason – shared with your EJB. Using the beanRefContext.xml which points to the applicationContext.xml means that you will instantiate a new application context and have no access to the Spring environment of the web instance.

I used the following method:

  1. Create a @Singleton annotated EJB inside the EJB package which holds the global Spring application context which is shared between EJBs and WAR:
    https://gist.github.com/schakko/6930091
  2. Create a @ManagedBean with eager loading. This bean is loaded on startup of the web application. A @PostConstruct annotated method initialies the Spring context:
    https://gist.github.com/schakko/6930116
  3. Create a new EJB interceptor which inherits from SpringBeanAutowiringInterceptor. The interceptor references the @Singleton annotated EJB for looking up the bean factory:
    https://gist.github.com/schakko/6930130

I am asking you for a donation.

You liked the content or this article has helped and reduced the amount of time you have struggled with this issue? Please donate a few bucks so I can keep going with solving challenges.