英文:
How to bind a spring bean to JNDI so that it can be accessed from a different war?
问题
我在wildfly中部署了一个WAR和一个EAR。EAR中有一个Spring Boot应用程序,它启动了一个特定的bean。我希望该bean的实例在JNDI中可用,类似于:
java:/mySpringBean
这样,我就可以从WAR中进行查找(WAR中同样有一个Spring上下文)。
目标是重用从EAR Spring Boot中实例化的实例。
首先,我不确定这是否可能,
我尝试过
https://konstantinpavlov.net/blog/2009/01/01/how-to-export-spring-managed-bean-to-jndi/
但似乎不起作用,我在向上述链接中添加代码后,在wildfly管理控制台的JNDI视图下看不到条目。
英文:
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
得分: 0
选择一个在你的Spring应用程序中进行引导的类。在我的情况下,有一个starupClass
,它会在应用程序启动时运行。然后我在那个类中使用@Autowired
来注入这个Bean。获取IntialContext
并绑定这个Bean。
Context ctx = new InitialContext();
ctx.bind("mySpringBean", mySpringBean);
英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论