Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Partial Dependency Injection, Partial Construction??

  1. #1
    Join Date
    Feb 2005
    Posts
    25

    Default Partial Dependency Injection, Partial Construction??

    Am I correct in assuming that Spring does not allow a bean to be
    partially configured using dependency injection and then completed
    by construction? That is, can one specify some dependencies for
    a bean in the configuration file and then, at run time, provide the
    rest of the dependencies by construction?

    I am imagining a call to the bean factory in which one provides
    an array of constructor arguments, something like this:

    ApplicationContext ctx = new .....
    Object[] conArgs = new Object[] { arg1, arg2,...}
    MyClass partDI = (MyClass) ctx.getBean("myClassBean", conArgs);

    I suppose one can use a lifecycle callback to set the remaining
    bean properties manually at runtime but this does not seem as clean as
    providing them to the bean factory.

    Any thoughts on this appreciated,
    -tom
    Balanced precariously on the learning curve

  2. #2
    Join Date
    Dec 2004
    Location
    Buenos Aires, Argentina
    Posts
    73

    Default

    I'm not sure if I get your point. Assuming some understanding, you can
    a. declare bean abean of class ABean in app context.
    b. IoC this bean in a let's say a service bean
    c. create a new ABean using a copy contructor.
    d. changing whatever you need to change in the new ABean instance.

    ABean class should hava a no arg constr. and a copy constr.

    Code:
    public class ABean implements MyBeanInterface {
      
    public ABean () {}
    
      public ABean (ABean original) {
           this.aProperty= original.getAProperty();
       }
    }
    in your service class create a new ABean instance passing to the constructor the IoC Abean instance.
    Code:
    ABean newBean = new ABean(this.injectedABean)
    Hope this helps,
    Regards,
    Gustavo.

  3. #3
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    How about a MyClassFactoryBean, which can take dependencies in the config file on behalf of MyClass, meanwhile can also call MyClass' constructor at runtime however you like?
    --Jing Xue

  4. #4
    Join Date
    Feb 2005
    Posts
    25

    Default

    Gustavo,

    Thanks for the reply and the interesting suggestion.

    If I understand your answer, it would seem to incur
    a lot of overhead: two beans for every one I really
    want. I've always been told that excess object creation
    is very expensive and should be avoided.

    Never-the-less your answer is very creative and
    worth thinking about. Thanks,
    -tom
    Balanced precariously on the learning curve

  5. #5
    Join Date
    Feb 2005
    Posts
    25

    Default

    Jing Xue,

    Thanks for the reply. This sounds very good but
    how is it done?

    The only example I have of using a factory bean
    comes from the book ProSpring. It shows how
    to create a factory for beans where the arguments
    which determine the bean are part of the configuration
    but not specified at runtime. That is, there is no example
    of providing any extra information to the bean at runtime.
    Is there a way to do this with a factory bean?
    regards,
    -tom
    Balanced precariously on the learning curve

  6. #6
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Quote Originally Posted by Tom Hicks
    The only example I have of using a factory bean
    comes from the book ProSpring. It shows how
    to create a factory for beans where the arguments
    which determine the bean are part of the configuration
    but not specified at runtime. That is, there is no example
    of providing any extra information to the bean at runtime.
    Is there a way to do this with a factory bean?
    In your original post, you imagined to be able to do:
    Code:
    Object[] conArgs = new Object[] { arg1, arg2,...} 
    MyClass partDI = (MyClass) ctx.getBean("myClassBean", conArgs);
    However you were planing to come up arg1, arg2, etc., well, just do the same in your FactoryBean. The only difference is on the second line above you would be directly calling MyClass constructor with all the args.
    --Jing Xue

  7. #7
    Join Date
    Feb 2005
    Posts
    25

    Default

    I'm sorry to be slow but I don't understand your answer.

    In your original post, you imagined to be able to do:
    Code:
    Object[] conArgs = new Object[] { arg1, arg2,...} 
    MyClass partDI = (MyClass) ctx.getBean("myClassBean", conArgs);
    However you were planing to come up arg1, arg2, etc., well, just do the same in your FactoryBean.
    OK, but I was planning to provide these arguments at runtime,
    as I created the bean. That's why I wanted to provide them
    directly to the 'getBean' method of the ApplicationContext.
    In other words, I can't wire them into the FactoryBean.

    The only difference is on the second line above you would be directly calling MyClass constructor with all the args.
    But, if I call the MyClass constructor directly with all the arguments
    then how is the BeanFactory involved at all? Are you suggesting that
    the code looks like this?:

    Code:
    Object[] conArgs = new Object[] { arg1, arg2,...} 
    MyClass partDI = new MyClass(conArgs);
    or perhaps even like this:

    Code:
    MyClass partDI = new MyClass(arg1, arg2,....);
    This is straight Java object creation and I don't see how the
    BeanFactory is involved at all. I need the BeanFactory to
    fill in the other, more static, parameters to my bean which
    are not given in the constructor.

    Anyway, sorry not to understand you. I would love to know
    a simple solution, like you hint at. Thanks,
    -tom
    Balanced precariously on the learning curve

  8. #8
    Join Date
    Dec 2004
    Location
    Buenos Aires, Argentina
    Posts
    73

    Default

    Tom,

    Thanks for your comments. Reading them, it comes up the issue about creating objects and I agree with the cost of creating objets. The thing here is Spring beans usually are service type of beans, I would not expect having thousands of Spring "incubated" beans. Usually they are singletons or sometimes prototypes. But, if the number of beans to be created is an issue, we must be talking about domain beans or should I say business domain objects, not service objects. Would you share with us your problem?.

    Thanks in advance,
    Gustavo.

  9. #9
    Join Date
    Feb 2005
    Posts
    25

    Default

    Quote Originally Posted by gfaerman
    Thanks for your comments. Reading them, it comes up the issue about creating objects and I agree with the cost of creating objets. The thing here is Spring beans usually are service type of beans, I would not expect having thousands of Spring "incubated" beans. Usually they are singletons or sometimes prototypes. But, if the number of beans to be created is an issue, we must be talking about domain beans or should I say business domain objects, not service objects. Would you share with us your problem?.

    Thanks in advance,
    Gustavo.
    Ah Gustavo, I must apologize for omitting to tell you that I was, indeed,
    talking about domain objects, not service objects.

    My question is general but imagine, as an example, that we have many clients who wish to log a message to the same database. I create a
    ClientLogger bean which needs a data source, but also requires the client name string and id number. I would like the Spring BeanFactory to wire-up
    the data source (since this is fixed) but then, at runtime, each client will create a new ClientLogger giving the logger bean its client name and id.

    Perhaps a FactoryBean can be used to do this, but I do not understand how. Or is there some other easy way to do it?
    regards,
    -tom
    Balanced precariously on the learning curve

  10. #10
    Join Date
    Dec 2004
    Location
    Buenos Aires, Argentina
    Posts
    73

    Default

    Tom,

    Thanks for sharing your requirement. If you need an instance of ClientLogger per each client, and you have a zillion clients, it seems you need a full blown whatever server. Time constrains wouldn't let me get deeper into it. But again, words like data source, client name, id number (and implicit pooling, threads etc), all of this smell to just one singleton service bean or a service facade, or aiming in some other direction, some prototype thing wrapped in a factory. I´m sorry I cannot help you at this time.

    Cheers,
    Gustavo.

Similar Threads

  1. Replies: 1
    Last Post: Aug 18th, 2005, 05:58 AM
  2. Replies: 1
    Last Post: Jul 4th, 2005, 01:02 PM
  3. Run-Time Dependency Injection (RTDI) idea.
    By jbetancourt in forum Container
    Replies: 3
    Last Post: May 4th, 2005, 05:00 AM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •