2014-11-03 69 views
4

我知道有类似于此问题的线程。下面是我的课,我在spring.xml文件中配置它。其实HumanResourceService是一个只有一个方法的接口。

无法实例化bean类:指定的类是接口

@Endpoint 
public class HolidayEndpoint { 

    @Autowired 
    private HumanResourceService humanResourceService; 

    @Autowired 
    public HolidayEndpoint(HumanResourceService humanResourceService) throws JDOMException { 
     this.humanResourceService = humanResourceService; 
    } 
} 

我的问题是,在我的spring.xml文件,当我定义HumanResourceService豆,它不能被实例化,因为这是一个接口。我如何在弹簧配置文件中提到一个接口。我的spring.xml文件低于

<bean id="holidayEndpoint" class="com.mycompany.hr.ws.HolidayEndpoint" autowire="constructor" > 
    <property name="humanResourceService" ref="humanResourceService" /> 
</bean> 
<bean id="humanResourceService" class="com.mycompany.hr.service.HumanResourceService" /> 

回答

5

你不能,Spring需要一些它可以创建实例的东西,界面不够。

在您的spring.xml中,id =“humanResourceService”的bean的class属性值应该是实现类的名称,而不是接口。 Spring需要你告诉它你希望它使用什么实现类。

+1

当我为id为“humanResourceService”的bean的类属性值提供为我的实现类时,我得到的错误号为 “找不到类型为[com.mycompany.hr.service.HumanResourceService]的限定bean依赖关系:期望至少有1个符合此依赖关系的autowire候选者的bean。依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:“ – Vaibhav 2014-11-03 20:02:40

相关问题