Announcement Announcement Module
No announcement yet.
Property is an interface, getting 'No Bean ... Defined' Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Property is an interface, getting 'No Bean ... Defined'

    I have a bean B which has a property I, which is an interface. It can be a number of other beans that implement I. When I run it says "No bean named 'I' is defined". So I then went in and tried to create a bean of the interface type I, but then I get "Could not instantiate bean class I: Specified class is an interface"

    What gives? How do I declare that a property can be any of the classes that implement interface I? I'm guessing it is something simple, but I don't see much on using interfaces in beans.xml in the docs. Did I overlook a section in the docs detailing on how to declare a property as being an object of an interface?

  • #2
    A bean can have a property whose type is an interface - this is very common with Spring (and other dependency injection frameworks). However, at the time that beans are instantiated and wired together, properties can only be set to concrete implementations of the interface. When instantiating a specific bean, a property can only be set to one concrete implementation, and you must specify which one.


    • #3
      OK, so I guess I simply leave out any mention of the bean property if I don't need it set during instantiation. I was under the impression that the bean.xml was to make the container aware all of the beans properties. I take it that the beans.xml is more of a configuration file then describing the beans.


      • #4
        I would say that beans.xml (as one way of configuring the Spring container) is used for declaring beans, wiring beans together, and for configuration. Whether a given property of a bean is required to be set by the Spring configuration or it is optional is really a matter of application design. However, if it is not important that a property is set by the Spring configuration, then why is the property in the bean?