如何将一个Spring Bean绑定到JNDI,以便可以从不同的war包中访问?

huangapple go评论90阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2020年9月9日 22:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63814198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定