Results 1 to 9 of 9

Thread: Setting public fields

  1. #1
    Join Date
    Aug 2004
    Posts
    16

    Default Setting public fields

    I need to set some public fields on a 3rd party class that does not quite meet the bean spec (no setX/getX). Is it possible to do this in Spring? More specifically, can it be done via declarative BeanFactory setup?

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    AFAIK Spring Framework does not support this type of injection. Did you try to extend your base class and provided getters/setters then use the new class in your application?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Posts
    16

    Default

    That was next Was checking to see if there was a 'built-in' way to handle this.

    Thanks

  4. #4
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    Also you may provide your own bean factory to adress this problem. Specify the property of your factory and simply inject it directly. This is also very easy to do. So you don't need to extend the class/modify it.

  5. #5
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    We could actually support "Field Injection" pretty easily in the core container. However, we don't think it's a good idea to encourage it. One of the aims of DI is to facilitate unit testing: populating an object through Setter or Constructor Injection in a JUnit test, for example. Field Injection is incompatible with that. Suddenly the container is doing magic that can't be done in plain Java (without reflection). IMO that's undesirable. DI is about the container parameterizing classes that aren't themselves reliant on magic.

    I agree that there may be a case for it to handle legacy code. But if we add this feature, people will use it in other cases...

  6. #6
    Join Date
    Aug 2004
    Posts
    16

    Default

    Totally concur that it's undesirable from a best practices POV, just the reality of wiring up code that's not 'yours'. Guess we need to think of ways to support but discourage it

  7. #7

    Default

    Quote Originally Posted by Rod Johnson
    One of the aims of DI is to facilitate unit testing: populating an object through Setter or Constructor Injection in a JUnit test, for example. Field Injection is incompatible with that. Suddenly the container is doing magic that can't be done in plain Java (without reflection). IMO that's undesirable. DI is about the container parameterizing classes that aren't themselves reliant on magic.
    I think you're misreading the original post, he is asking about public field injection, such behavior can easily be reproduced in test cases without reflection. Still not currently a very good pratice though, just for different reasons, such as encapsulation. I guess the popularity of AOP could make public fields come back though, but we're not there yet.

  8. #8
    Join Date
    Aug 2004
    Location
    Herndon, Virginia
    Posts
    28

    Default Re: Setting public fields

    Quote Originally Posted by eoliphant
    I need to set some public fields on a 3rd party class that does not quite meet the bean spec (no setX/getX). Is it possible to do this in Spring? More specifically, can it be done via declarative BeanFactory setup?
    Couldn't you wrap the class? Or wrap it and have the wrapper class implement the same interface?
    ---------------------------------
    David H. McCoy


    ----------------------------------

  9. #9
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Yes--I did misread the original post, which specified public fields. I like the suggestion about enabling but discouraging it. Perhaps through a config parameter that needs to be set specially to permit Field Injection.

    It would be nice to have Spring support all possible kinds of injection--we already have Constructor, Setter and Method Injection. However, I would hate to see this practice become considered to be normal Spring usage.

Similar Threads

  1. Forms
    By sherihan in forum Web
    Replies: 4
    Last Post: Oct 2nd, 2006, 06:24 AM
  2. Replies: 7
    Last Post: Aug 18th, 2005, 02:41 PM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Questioning the core component
    By Martin Kersten in forum Swing
    Replies: 6
    Last Post: Feb 21st, 2005, 03:45 AM
  5. Enlisting Custom DataSource with JTa
    By shaby775 in forum Data
    Replies: 11
    Last Post: Sep 9th, 2004, 03:00 AM

Posting Permissions

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