0

I want to inject object that has 12mb into my jetty webapp. I deploy my app as WAR file so I cannot add guice injector as it is shown in other question.

How to do it? I quess I need to use applicationContext.xml

Aleksander Zendel
  • 463
  • 1
  • 3
  • 12
  • Looks like you're already using spring, where you can create singleton objects and provides object injection. What's your real problem? – Luiggi Mendoza May 06 '15 at 14:35
  • I am new to java ecosystem, I know that jetty uses spring, I don't know how exactly it works. Could you give me link about spring singletons? – Aleksander Zendel May 06 '15 at 15:04
  • Jetty does not use Spring. In fact, its often setup the other way around, Spring is setup to initialize and start Jetty. – Joakim Erdfelt May 06 '15 at 15:09
  • 1
    Jetty doesn't use spring. Your application may use Spring if you have configured it to do so. Please add all the info you think relevant for us to understand how your project currently works so we will be able to provide more help. – Luiggi Mendoza May 06 '15 at 15:10
  • (+1) to the _"Please add all the info you think relevant for us to understand how your project currently works"_ – Paul Samsotha May 06 '15 at 15:30

1 Answers1

2

Use the ServletContext.setAttribute(String,Object) to store your object.

Then use ServletContext.getAttribute(String) to access that singleton from your Servlets and Filters.

Make sure your object is safe to use from multiple threads!

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Downside on this approach: you should inject the object at application startup and you can only recover the object through a request/response cycle. This means that you cannot obtain this *singleton* in an asycn operation done in the server e.g. cron fired every X minutes. Also, since OP posts that he/she uses applicationContext.xml it's a possibility the app uses Spring and OP doesn't know how to take advantage of the benefits (?) of this framework. – Luiggi Mendoza May 06 '15 at 15:09
  • If you are using Jetty, and you are triggering something, anything, in the webapp, you have access to the `ServletContext`, either via normal servlet means, or via the Jetty `WebAppContext`, or via the Jetty `ServletContextHandler`, or via the Jetty `Server` object. – Joakim Erdfelt May 06 '15 at 15:11
  • Cron or any other async operations do not need to know any of these objects. And neither of your model classes in the app should. – Luiggi Mendoza May 06 '15 at 15:14
  • The implementation in Jetty for `ServletContext` is the Jetty specific `ServletContextHandler`, which there exists 1 and only 1 per webapp. This information can be accessed by anything in the webapp, be it triggered via an http request, artificial local connector request, anonymous request dispatcher, jmx call, timed event via scheduler, server bean calls, lib callbacks, jms queue, etc (list of possible ways is endless). In short, any code, in that webapp, can access the that singleton, with or without a request, and with our without an IoC framework like Spring/Guice/CDI, etc. – Joakim Erdfelt May 06 '15 at 15:25