This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I converted my current application from struts1 to struts2 a few months back. One of the places where I lost the most time was trying to figure out how to load the applications reference data into an object that would allow me to access the data from memory rather than accessing the database. I wanted to do this on start up so I used a configuration provider class. The problem I ran into was that I wanted to use injection to get some values into the class and many of the methods in the class that I tried to use couldn't access the injected objects. I'm assuming that the part of the framework that handles injection was running by the time these methods were called. I was able to make it work by putting my code into the loadPackages() method. Based on the method name this doesn't seem like the right place to do this kind of logic but it was the only way I found to make it work.
1. Is there a more appropriate place for this kind of code? 2. Apparently this kind of processing is a little obscure becuase I couldn't find good explanations on how to make a configuration provider online or in any books at my local B&N. Does this new book cover in depth topics like this?
The specific answer is no, the book doesn't cover topics like this--it's pretty application- and environment-specific.
The broader answer would be that this type of data generally goes into the application context, which is then available through a plain old Java map to actions that implement ApplicationContextAware.
If you want to inject specific application-scoped objects into an action you'd probably want to create a custom interceptor that checks for marker interfaces (or annotations) and upon finding them injects the appropriate objects via setters.
Joined: Aug 29, 2008
I'm not sure how to do this via the context. I have a large amount of ref data in my database which does change on occasion but not often. I want to load it on start up of the application. What is the best way to perform an action like this on start up of the application?
Well, there's lots of answers. I default to just using a typical application context startup listener, which isn't S2-specific, and covers typical needs. When it changes I just update the app context object; for client-focused apps (like Flex/etc.) I set a "dirty" flag that is checked for by the client.
(Again, that's just me, and I could me totally misunderstanding your needs!)
Joined: Aug 29, 2008
I'm always glad to hear about another way to do something (especially when I'm not that comfortable with the way I have it). Thanks for the help.