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

问题描述 投票:0回答:2

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

@Endpoint
public class HolidayEndpoint {

    @Autowired
    private HumanResourceService humanResourceService;

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

我的问题是,在我的 spring.xml 文件中,当我将 HumanResourceService 定义为 bean 时,它无法实例化,因为这是一个接口。我如何在 spring 配置文件中提及接口。我的 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" />
spring
2个回答
10
投票

你不能,Spring 需要一些可以用来创建实例的东西,接口是不够的。

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


0
投票

我觉得你应该改变

<bean id="humanResourceService" class="com.mycompany.hr.service.HumanResourceService" /> 

<bean id="humanResourceService" class="com.mycompany.hr.service.HumanResourceServiceImpl" />

© www.soinside.com 2019 - 2024. All rights reserved.