#Beginner Spring framework no qualifying bean exception

39 messages · Page 1 of 1 (latest)

worldly cradle
#

I'm taking a Spring framework course and I'm having a lot of trouble with this as it seems to be correct from what the course is showing me. I keep getting a exception saying

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.in28minutes.spring.learnspringframework.GameRunner' available"

Main class

@SpringBootApplication
public class LearnSpringFrameworkApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context;
        context = SpringApplication.run(LearnSpringFrameworkApplication.class, args);

        //MarioGame game = new MarioGame();
        //PacmanGame pac = new PacmanGame();

        //GameRunner runner2 = new GameRunner(game);
        GameRunner runner = context.getBean(GameRunner.class);
        runner.run();
    }
}

Game Runner class, the one that is considered "not a qualifying bean"

@Component
public class GameRunner {

    @Autowired
    private GamingConsole game;

    public GameRunner(GamingConsole game) {
        this.game = game;
    }

    public void run() {
        game.up();
        game.down();
        game.left();
        game.right();
    }
}

Gaming Console Interface

@Component
public interface GamingConsole {
    void up();
    void down();
    void left();
    void right();
}
dapper thicketBOT
#

This post has been reserved for your question.

Hey @worldly cradle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

worldly cradle
#

Beginner Spring framework no qualifying bean exception

molten fulcrum
#

An interface can't be a component. Beside the automatic data CRUD repositories, what would that even mean?

worldly cradle
#

I was mostly just trying things to see if they worked

#

doesn't work with or without the component on the interface anyway

#

The mariogame and pacman game classes commented out implement the game console interface

molten fulcrum
#

You do need an implementation that ca, be autowired

#

It's juste the interface itself isn't an implementation

#

You could make MarioGame a component, if it implements it

worldly cradle
#

here's the mariogame class

@Component
public class MarioGame implements GamingConsole {
    public void up() {
        System.out.println("up");
    }
    public void down() {
        System.out.println("down");
    }
    public void left() {
        System.out.println("left");
    }
    public void right() {
        System.out.println("right");
    }
}
#

I did give it a component annotation

molten fulcrum
#

okay, apparently the error is that it doesn't find GameRunner itself. Dang

#

Where are the classes?

worldly cradle
#

I did move them around trying to fix the exception, but originally the MarioGame, GameRunner, and GameConsole classes were in a package next to the main class

#

here's a screengrab

molten fulcrum
#

this shouldn't provoke that error. I wonder if you can actually use the context given by run() this way

worldly cradle
#

I thought maybe it was a problem with my configuration maybe but it was all set up using spring initializer

worldly cradle
molten fulcrum
#

So maybe the context given by run() isn't meant to be used that way

worldly cradle
#

That was what the guy in the course did at least
what would I do if I wanted to get the GameRunner bean then?

molten fulcrum
#

No idea, because you wouldn't want the GameRunner bean. You would want it to be used by your application as it runs

#

Think about when you make a REST server with Spring Boot, it's just made of controllers that respond to HTTP requests. The controllers autowire what they need and are run by Spring Boot itself. You never ask for the beans in any way

#

Usually that's how Spring Boot works

worldly cradle
#

Hmm

molten fulcrum
worldly cradle
molten fulcrum
#

Guess that context isn't meant to be used that way

worldly cradle
#

dang

#

I have no idea what this guy in the course is doing then

#

this is what he's getting

#

console statements from mario class

molten fulcrum
#

A very different version, maybe...

#

I guess I could try if I had the original material

worldly cradle
#

Thanks for the help

dapper thicketBOT
# worldly cradle Thanks for the help

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

worldly cradle
#

hope you have a good one