Announcement Announcement Module
Collapse
No announcement yet.
Expressing time-sequence, not object-reference dependencies Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Expressing time-sequence, not object-reference dependencies

    I'm using a combination of WebWork2 and Spring. In WebWork2 I'm using the "external-ref" method of referencing Spring objects in my Action.

    I've finally got Spring wiring up all four classes. But there's still one thing I'd like to do that I can't figure out:

    Here's the deal:

    I've got Four classses
    * Datasource
    * Extractor
    - uses an datasource for db connections
    - uses a model to store extracted data in
    * Model
    * Action (WebWork2 action)
    - uses a "populated" model to grab data from

    Mostly this is all good. Spring is injecting the datasource and the Model into the extractor. And WebWork2/Spring are injecting the Model into the Action.

    But my Model is coming through to the Action empty. The Extractor has not run (because nothing has referenced it) so I just end up with a blank Model.

    My Action is dependent upon the Extractor to have populated the Model with data from the databse before it uses it. But the Action doesn't need a ref to the extractor, it needs a ref to the Model, so nothing in the Action is triggering the Extractor to run.

    The Model has no direct depency on the extractor (and I don't want it to), it's just a set of objects that hold data. It gets used by the extractor not vice-versa (it doesn't use or know about an extractor).

    But the Model does have a sort of indirect depency in that the Model isn't really a helpful object (it's empty) until the extractor has run.

    So how can I express to Spring that I need the Extractor to have run before anybody (my Action) grabs a ref to the Model?

    I tried putting a "depends-on" clause on the Model which, at first blush, seemed to be what I wanted. But after further thought, this would seem to cause a circular reference since I'm saing the extractor needs an instance of the model, and that the model indicates the extractor should run before the model is instantiated. For whatever reason (circular reference or no) it didn't work.

    So I've come to the conclusion that I don't have an "object dependency" that I'm trying to express but rather a time-sequence depenency that I'm trying to express (the extractor should run before refs to the Model are handed back). I have know clue how to do that.

    I'm completely willing to accept that maybe I've created an object model here that's less than ideal and that refactoring would be more direct-inject friendly.

    Any help is greatly appreciated!

    Many Thanks!

    - Gary

  • #2
    Well, I thought I found what I needed in the docs. Section 3.3.1 says:

    This potentially delayed visibility of some configuration issues is why ApplicationContext by default pre-instantiates singleton beans. At the cost of some upfront time and memory to create these beans before they are actually needed, you find out about configuration issues when the ApplicationContext is created, not later. If you wish, you can still override this default behavior and set any of these singleton beans to lazy-load (not be preinstantiated)
    So I rewrote my Extractor to use constructor injection and then called the "extract" method right from the construction.

    I had hoped that the Extractor object would be instantiated right away (per the docs above) but that doesn't seem to be happening.

    The extractor is declared as a singleton (I've tried both explicit singleton declaration and just letting the default do its thing). The AppContext is created as a result of the Servlet listener I configured in Web.xml (per the docs).

    Hmmm.

    So I guess I'm still looking for help on this.

    - Gary

    Comment


    • #3
      Ignore that last comment. Lazy instantiation (or in this case, the lack of it) is working fine. The problem seems to be in how I'm handling object references in various places.

      At this point I'm thinking the problem is me.

      I'll write back with the solution when I get this figured out.

      - Gary

      Comment


      • #4
        Assuming the Extractor initializes the model when it's instantiated (either in the constructor, or by an initialize method which you can specify in the bean config), then would your requirements be satisfied by having the Action 'depends-on' the Extractor?
        As I understand it, that would ensure the Extractor is instantiated before the Action, thereby populating the Model before the Action gets it.

        Comment

        Working...
        X