How to bind a spring bean to JNDI so that it can be accessed from a different war?

Viewed 226

I have a WAR and an EAR deployed in wildfly. The ear has a spring boot application which spins up a particular bean. I want that instance of the bean to available in jndi something like

java:/mySpringBean

so that I can do a lookup from the WAR (which again has a spring context). The goal is to reuse the instance which was instantiated from the EAR spring boot.

First of all, I'm not sure if that's even possible, I tried https://konstantinpavlov.net/blog/2009/01/01/how-to-export-spring-managed-bean-to-jndi/ but it doesnt seem to be working, after I added the code from above I do not see a an entry in wildfly management console under the JNDI view.

1 Answers

Pick one of the classes where your spring application is bootStrapping. In my case there was a starupClass which would run upon application start. Then I @Autowired the bean to that class. Got the IntialContext and bind the bean.

Context ctx = new InitialContext(); ctx.bind("mySpringBean", mySpringBean);

Related