Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to store an object in 'application' scope in Spring when the server is started?  RSS feed

 
Anand Jayabalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to get some data from the master tables in the database and store them in the application context at app startup.

I created a class named MasterDataLoader and set the DAO object in it using dependency injection. Defined a method named "initialize()" to call the DAO methods to get the data from the database. In the bean declaration in the applicationContext.xml, I gave the init-method as "initialize". So when the app starts it makes the database calls and get the data. I just want to how to store it in application scope.

Also, how to access such an object (in application scope) in a Controller?

Appreciate any help.

Thanks,
Anand
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why store it in web application scope? You can just add a "setMasterDataLoader(MasterDataLoader)" method on your controller and inject the class in the Spring config.
 
Anand Jayabalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I can do that. Didn't strike my mind

Thanks a lot, Nathan.

I still have one question though. If I need to access it in multiple JSP pages, what's the best way to do it? Is it a good idea to set it in session scope in the formBackingObject() or handleRequestInternal()?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add it to session scope anywhere - as long as you can get a reference to the session object through request. As to whether you should use it - that depends - are these JSPs all backed by Spring controllers? Is the data different for different users or same for all users?
 
Anand Jayabalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. All the JSPs are backed by Spring controllers. And the data is same for all users.

Truly appreciate your time,
Anand
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't store it in session scope then - you'd be wasting memory doing this and it would make your app less scalable. You could (again) add methods and wire up a reference to the MasterDataLoader in each controller... I'd do this if I needed access to the data in the controller. If you just need to display it on the JSP page, and don't need it in the controller doing what you originally planned and putting it in application scope would probably be easier.
 
Anand Jayabalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. But now this brings me back to my original question (in the first post in this thread): how to store an object in application scope?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry it's taken so long to get to the answer you wanted all along

You'll want to make MasterDataLoader implement SerlvetContextAware :

 
Anand Jayabalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'da man, Nate! This worked just fine and this is exactly what I was looking for Appreciate your help.

Another doubt came up in my mind though when I implemented this. What would be the best way access such an object (viz., "myKey") from a Controller?

Since I have created a bean entry for MasterDataLoader in the config xml for the setServletContext() method to be called, can I just inject the masterDataLoader into the Controller or is it possible to get the object directly from the application scope (if so, how?)? Which is better practice?

Thanks
 
Linh Dang Hong
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Injecting the masterDataLoader into Controller is the better way, I think. Putting so many thing in ServletContext would make your webapp too complex to maintain. Because masterDataLoader is Spring bean, so you can configure its scope in XML configuration file, as described in this link
http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes-singleton
Let Spring do its job
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spring controllers (at least the web ones...) implement ServletContextAware and WebApplicationContextSupport - so you can just call getServletContext().getAttribute("myKey") inside your controller to get to the object stored in application scope. However, I agree with Linh Dang Hong (which is why I asked so many questions before just giving the answer) if your controller has a dependency on MasterDataLoader, it's better to explicitly declare this through spring rather than just grabbing the data out of the application context.
 
Anand Jayabalan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Linh and Nate. I'll explicitly inject the bean into the Controller rather than getting it from the app context.

I learn a lot from your explanations. Thanks much.

-Anand
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!