Announcement Announcement Module
No announcement yet.
delaying/rippling a dependency Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • delaying/rippling a dependency

    I've searched for an answer to this question without success, so I'll post the problem I'm facing. It's simple really, but how do I do this?

    I have a DAO that needs a DataSource. Then, I have a Service that uses this DAO. Finally, I want to provide the Service to other users, and let them specify the DataSource. How do I "forward" the DataSource that the user provided, to the DAO?

    If my design is wrong, do not hesitate to point this out and suggest a better design.

    Ideally I want the user to be able to do this:

    <bean id="myService" parent="service">
      <property name="dataSource" ref="dataSource"/>
    where the user has defined his own "dataSource" definition. Then they can inject "myService" wherever they need to use it.

    In the module I am providing, I have:

    <bean id="userDao" class="p.UserDaoImpl">
      <!-- property name="dataSource" ref="dataSource"/ -->
    <bean id="service" class="p.Service" abstract="true">
      <property name="userDao" ref="userDao"/>
    So userDao needs a DataSource to be configured, but I want this to be left "abstract" until the external user provides it to the service. When that happens, I want to "forward" this dataSource to the userDao. I can't figure out how to do this.

    Any help or suggestion of a better approach to achieve this result would be greatly appreciated.

    Thanks in advance.


  • #2
    Have you had a look at this? Might give you some ideas.


    • #3
      Thanks for your fast and helpful reply!

      At first reading it doesn't look like exactly what I need but as you said, the goal was to give me some ideas and that it does. I will look more into it and I think that I'll be able to figure something out, based on the explanations to which you refer.

      If I come up with an elegant solution to my problem I'll post it back here. In the meantime, any other suggestions are most welcome.

      Thanks again.



      • #4
        Not a problem, hope it was of some help to you! I would be very interested to hear what you come up with! There was another blog posted a while ago that proposed something similar, but with a different implementation. Might also be useful.


        • #5
          Hi again,

          Here's a possible solution: I use a proxy (wrapper, delegate, whatever you want to call it) DataSource in my module, and when the user specifies the real datasource, I forward this definition to the proxy.

          I tried using Spring's DelegatingDataSource, but it's not happy about not having a target DataSource at construction time.

          So I wrote my own wrapper which just forwards all calls to the target, but accepts not having the target specified at construction time. It gets specified when the user supplies it.

          I didn't look into Spring's other classes, perhaps there's one already that achieves this.

          Generally speaking this seems to be an acceptable solution to forward a dependency that gets specified outside of a module.

          What do you think?



          • #6
            I guess I could simply extend DelegatingDataSource and override afterPropertiesSet() not to throw an exception..