Announcement Announcement Module
No announcement yet.
Out of one bean, into another Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Out of one bean, into another

    I have a bean that does some work. The work it produces is an object (class Foo) that another bean would like to consume. I don't want the consumer to have special knowledge of the producer.

    I know that I can add an interface (FooSource) and make my producer implement it and make my consumer consume it, and make any other sources of Foo also produce it. It's clumsy, though, because I can otherwise create some of my Foos as beans.

    Another path would be to treat the source as a factory bean for Foo objects, and declare the produced item as produced by the factory.

    What I keep thinking I should be able to do is something like:

    <property name='wantAFoo' value='sourcebean.getAFoo()'/>

    or some equivalent. Am I missing something?

  • #2
    Producing it via factory to me seems the way to go. If you want Foo to be spring managed beans then inject them into the factory but then why wouldn't you inject directly into the consumer?


    • #3
      Making this concrete

      In the below, there is no smoothedModel bean until the smoother creates it. So the only way I can see to inject it is to use a factory as I have done. I suppose the factory notation is just a verbose way of saying 'pull a bean from method X of bean Y'.

      <?xml version="1.0" encoding="UTF-8"?>

      <bean id='model'
      class='com.basistech.excedrin.model.SimpleModelFac tory'
      <constructor-arg value="file:${modelPathname}"/>

      <bean id='corpus' class=' '>
      <property name='model' ref='model'/>
      <property name='input' value='file:${corpusPathname}'/>

      <bean id='smooth' class="com.basistech.excedrin.reestimate.Estimated SmoothedModelTask">
      <property name="model" ref="model"/>
      <property name="corpus" ref="corpus"/>

      <bean id="smoothedModel" factory-bean="smooth" factory-method="getResultModel"/>

      <bean id="writer" class="com.basistech.excedrin.model.SmoothedModelW riterTask">
      <property name="model" ref="smoothedModel"/>
      <property name="output" value="${smoothedModelPathname}"/>

      <bean id="tasks" class="com.basistech.excedrin.TaskSequence">
      <property name="tasks">
      <ref bean='corpus'/>
      <ref bean='smooth'/>
      <ref bean='writer'/>



      • #4
        Not sure if this is exactly what you need, but you may inject a common data bean in both producer and consumer:

        <bean id="item" class="example.item" />

        <bean id="producer" class="example.Producer">
        <property name="produceditem" ref="item"/>

        <bean id="consumer" class="example.Consumer">
        <property name="item" ref="item"/>

        This way, once the producer has finished it's work, the consumer may use the result, as the item is the same object for both

        The bad part, is that the system is a bit limited:

        a) item should have a flag telling if it is already processed or not.
        b) It makes really difficult to use several producers in paralell