开发者

How to obtain singleton's intance in JBoss?

I have a singleton that I hav开发者_StackOverflow社区e to instantiate in my filter and for some reason I cannot get it's instance on couple first attempts. (The application deployed in JBoss EAP 5.1) This same solution with no modification works just fine with Jetty Application server. Greatly appreciate for your answer in advance.

Sincerely,

Roman


If you don't want to use any dependency injection framework ( Spring, Guice ), do it the old fashion way:

Install ServletContextListener

In contextInitialized(ServletContextEvent sce) method create your singleton and store it as an attribute on ServletContext

When your filter will be initialized you should be able to get that singleton instance from the ServletContext that is passed inside the FilterConfig interface in your filter's init method


Why do you need a singleton in your filter? You have web-app, session, and request contexts to put in any data you wish.

That said, without having seen your code it is ~100% likely you are using a class static variable to hold an instance? If yes, please note that classes are scoped by class-loaders, and this is one among many reasons why it is a bad idea to resort to such idioms in an application server (which pretty much is guaranteed to be implemented using class-loader hierarchies, etc.) JBoss and Jetty clearly have different approaches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜