春季如何解决“ Coach无法解析为类型”错误

时间:2019-09-17 12:26:05

标签: java spring interface javabeans

我是Spring的新手,我有包含bean的applicationContext.xml,我使用ClassPathXmlApplicationContext类加载了bean。然后,我还有一个名为Coach的接口,带有方法getDailyWorkout()和一个名为TrackCoach的类,该类实现Coach并覆盖方法getDailyWorkout()。当我使用 Coach theCoach = context.getBean("myCoach", Coach.class); 出现错误

    Coach cannot be resolved to a type
    Type mismatch: cannot convert from Coach to Coach
    Coach cannot be resolved to a type

我已经尝试用不同的值编辑bean,并且还尝试阅读文档,我很确定我以正确的方式使用了代码。我已经尝试过将强制类型转换添加到context.getBean()方法中。我是Spring的新手,所以请帮助我使用此代码。

这是我的bean在文件applicationContext.xml中

<bean id="myCoach" class="com.schitiz.springdemo.TrackCoach"></bean>

这是我的跑步者班级文件,主要是

package com.schitiz.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpringApp {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Coach theCoach = context.getBean("myCoach", Coach.class);
        System.out.println(theCoach.getDailyWorkout());
            context.close();
    }
}

我的TrackCoach课是

package com.schitiz.springdemo;
public class TrackCoach implements Coach {
    @Override
    public String getDailyWorkout() {
        // TODO Auto-generated method stub
        return "Run 5 kms";
    }
}

输出必须为:运行5公里。但显示错误

0 个答案:

没有答案