Bean named 'myCoach' is expected to be of type 'springboot.practice.Coach' but was actually of type 'springboot.practice.MyApp'

Issue

I am new to springboot, I changed the class to ‘springboot.practice.Coach’ and still got an error.

Here is my applicationContext.xml code

<?xml version="1.0" encoding="UTF-8"?>
   <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
            
            <bean id="myCoach" 
                  class="springboot.practice.MyApp">

            </bean>
   </beans>

here is the main class
package springboot.practice;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringHello 
{
    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();
        
        System.out.println("dasdsadas");
    }
}

Im stuck here for almost 3 hours.

Solution

When you add this to your configuration:

<bean id="myCoach"  class="springboot.practice.MyApp">

You are instructing spring IoC container to expect bean of class MyApp, Change this class to springboot.practice.Coach (ot correct classpath) and it should be resolved.

Answered By – Mihael Mihov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published