Announcement Announcement Module
Collapse
No announcement yet.
@Autowired: how to wire an interface type? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired: how to wire an interface type?

    When I'm trying to autowire an implementation class of an interface type into my controller, deployment will failed.
    This is my java class

    @Controller
    public class AdminHomeController {

    private EmicroDelegate delegate; // it's an interface type

    public AdminHomeController() { }

    @Autowired
    public AdminHomeController(@Qualifier("emicroDelegate") EmicroDelegateImpl delegateImpl) {
    delegate = delegateImpl;
    }
    .....
    }

    this is in my emicro-servlet.xml:
    <context:annotation-config/>

    <bean id="emicroDelegate" autowire="byName" class="mycom.delegate.EmicroDelegateImpl" />

    <bean class="controller.AdminHomeController">
    .......

    The error message is:
    Context initialization failed
    org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'adminHomeController': Unsatisfied dependency expressed through constructor argument with index 0 of type [mycom.delegate.EmicroDelegateImpl]: No unique bean of type [mycom.delegate.EmicroDelegateImpl] is defined: Unsatisfied dependency of type [class mycom.delegate.EmicroDelegateImpl]: expected at least 1 matching bean


    It drives me crazy now. Anyone can help? Thanks a lot

  • #2
    Shouldn't the parameter in the constructor be of the interfacetype to? Bit strange to have an interface in your class but wire the concrete implementation.

    Have you tried with component scan instead of defining the bean yourself? Also your bean definition looks like it contains more, can you post the whole bean definition?

    And please next time use [ code][/code ] tags (without the spaces)...

    Comment


    • #3
      Shouldn't the parameter in the constructor be of the interface type to?
      Only after it failed to use interface type did I use concrete implement class. Unfortunately it failed, too.

      Yes, as you can see it is a delegate class, which hide the old-school EJB2 bean. The reason to "code the interface" is obvious. The implement of this delegate interface may change -- and we're going to change it since we are trying to convert EJB2 to web service, but don't want to disrupt web front end, therefore an interface should be more appropriate.

      the bean class is simple
      Code:
      @Controller
      public class AdminHomeController {
      	private final Log logger = LogFactory.getLog(getClass());
      
      	private EmicroDelegate delegate; // EmicroDelegate is interface type
      	
      	public AdminHomeController() {	}
      	
      /* I tried use both EmicroDelegate and EmicroDelegateImpl as parameter, but failed */
      	@Autowired
      	public AdminHomeController(@Qualifier("emicroDelegate") EmicroDelegateImpl delegateImpl) {
      		delegate = delegateImpl;
      	}
      
      
          @RequestMapping("/checkin.html")
      	public String listOrders2Checkin(ModelMap model) {
      List<SampleFront> sampleFronts = new ArrayList<SampleFront>(delegate.getSamplesForCheckIn()); int samplesNbr = sampleFronts.size(); model.addAttribute("totalSamples", samplesNbr); model.addAttribute("samples", sampleFronts);
      return "checkin-view"; } }
      I haven't tried component-scan yet, maybe I need to try. However I did try to use old style bean definition in emicro-servlet.xml:
      Code:
      <bean class="mycom.controller.AdminHomeController">
      		<property name="delegate">
      			<bean class="mycom.delegate.EmicroDelegateImpl"/>
      		</property>
      </bean>
      then use a setter method in AdminHomeController.java,
      Code:
      public void setDelegate(EmicroDelegate delegate) {
              this.delegate = delegate;
      }
      it worked. Don't know why the annotation won't work

      Comment

      Working...
      X