Results 1 to 6 of 6

Thread: autowiring by type (AbstractTransactionalDataSourceSpringContextTests )

  1. #1

    Default autowiring by type (AbstractTransactionalDataSourceSpringContextTests )

    Hi!

    Spring 2.0 RC1 build 44.

    I've followed the petclinic example that comes with the distribution to implement a simple DAO using Spring's JdbcDaoSupport.

    So there is an interface MyDao and an implementation MyJdbcDaoImpl.

    I then created a test case that inherits from AbstractTransactionalDataSourceSpringContextTests and I get an exception when executing this test case.

    Error creating bean with name 'MyJdbcDaoImplTest': Unsatisfied dependency expressed through bean property 'myDao': There are 2 beans of type [interface MyDao] for autowire by type....

    Now I'm a little bit confused. There are two types but one is an interface. Why is Spring unable to resolve this?

    Any ideas?

    Thanks,
    KM

  2. #2
    Join Date
    Feb 2006
    Location
    Arlington, VA, USA
    Posts
    194

    Default

    I guess the best answer to your question may ironically be a non-answer: Rob Harrop, in his ProSpring book (p. 87) does recommend avoiding autowiring to begin with. ("For any non-trivial application, steer clear of auto-wiring at all costs.")

    Perhaps the next best answer may also be a non-answer: Why do you need to declare a bean that is just an interface (and not an actual class)? What use would it serve?

    Finally, the third best answer may be the real answer for you here: Once you determine the use case for having an interface as a bean, that is probably a reason why Spring cannot simply ignore bean-interfaces and just go with the bean-class while autowiring (and further, only raising a complaint if more than one *class* of a given type is declared.)

    You seem to be saying on one hand, that you should be able to declare interfaces as beans alongside classes implementing them because they serve a useful purpose, and, on the other, that Spring should be ignoring them when matching by type because interfaces aren't useful as beans. It seems to be a contradiction in thinking here, unless I am missing something (*very* possible here.)

    Glen

  3. #3

    Default

    I don't know. I'm just referring to the sample code that is shipped WITH Spring.

    And the advantage of this abstract test case is, that is very simply to adapt. You point to the applicationContext.xml from within the test case and that's it. It's very simple and easy to use.

    Anyway, I just think that the problem could be avoided in this case because it is really simple. There is only one interface and one implementation. But I agree that it gets ambigious when there are multiple implementations.

    I could workaround by making the getMyDao abstract in the test case and having a MyJdbcDaoImpl specific testcase that gets wired by type nicely.

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Now I'm a little bit confused. There are two types but one is an interface. Why is Spring unable to resolve this?
    Your setter probably accepts the interface so there are two objects (two interface implementations) that can be passed to the setter. That's why Spring doesn't know which one to choose.
    Autowiring is something that should not be used in production environments in my opinion but it comes in handy when in tests since you don't have to build separate application contexts for your tests.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5

    Default

    Quote Originally Posted by Costin Leau
    Your setter probably accepts the interface so there are two objects (two interface implementations) that can be passed to the setter.
    Yes, that's how it's implemented in the PetClinic example. And because it is done in the PetClinic example I assumed it's the suggested way of doing tests that way. Maybe I shouldn't trust too much in the examples.

    Anyway, I changed it now and it works.

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Yes, that's how it's implemented in the PetClinic example. And because it is done in the PetClinic example I assumed it's the suggested way of doing tests that way. Maybe I shouldn't trust too much in the examples.
    Using the lowest common element is the best way to go. You can do the wiring by yourself or select a different wiring type (by name for example). Testing should not affect the way you code - it should be the other way around.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

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