No Qualifying bean of type helps with this error:

Asked

Viewed 240 times

0

I’m trying to use the swing and the spring but I have this problem:

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.market.controller.LoginController' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:346)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:337)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
    at com.market.app.MarketApplication.main(MarketApplication.java:17)
    ... 5 more

my main:

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.market.controller.LoginController;

@SpringBootApplication
@EnableAutoConfiguration
public class MarketApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(MarketApplication.class).headless(false).run(args);
        LoginController appLogin = context.getBean(LoginController.class);
    }

}

my controller:

package com.market.controller;

import java.util.List;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.market.action.AbstractAction;
import com.market.model.Funcionarios;
import com.market.ui.LoginFrame;



@Controller
public class LoginController extends AbstractController {
    @Autowired
    private LoginFrame frame;



    private void init() {
        this.frame.addWindowListener(this);
        registerAction(frame.getbEnter(), new AbstractAction() {
            private List<Funcionarios> list; 

            @Override
            public void action() {
                if (!frame.getjLogin().getText().trim().isEmpty() || !frame.getjPassword().getText().trim().isEmpty()) {

                }else {
                    JOptionPane.showMessageDialog(frame, null, "Campo vázio", JOptionPane.INFORMATION_MESSAGE);
                }
            }

            @Override
            public void posAction() {
                cleanUp();
            }
        });
    }

    public void show() {
        frame.setVisible(true);
    }

    @Override
    protected void cleanUp() {
        frame.setVisible(false);
        frame.resetForm();

        super.cleanUp();
    }
}

my view:

package com.market.ui;

import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jnativehook.keyboard.NativeKeyEvent;
import org.springframework.stereotype.Component;

@Component
public class LoginFrame extends JFrame {

    private static final int FRAME_WIDTH = 400;
    static final int FRAME_HEIGHT = 350;
    static final int FRAME_X_ORIGIN = 150;
    static final int FRAME_Y_ORIGIN = 150;
    static final int BUTTON_WIDTH = 90;
    static final int BUTTON_HEIGHT = 30;
    private JTextField jLogin;
    private JTextField jPassword;
    private JButton bEnter;
    private JButton bRegister;
    private JPanel loginForm;
    private JLabel loginText;
    private JLabel passwordText;

    public LoginFrame() {
        setTitle("Login System");
        setSize         (FRAME_WIDTH, FRAME_HEIGHT);
        setLocation     (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        setResizable(false);
        initComponents();
    }
    private void initComponents() {
        loginForm = new JPanel(new GridLayout(3,1));
        //buttons
        bRegister = new JButton("Registrar");
        bRegister.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT);
        bEnter = new JButton("Registrar");
        bEnter.setBounds(195, 285, BUTTON_WIDTH, BUTTON_HEIGHT);
        bEnter.setMnemonic(NativeKeyEvent.VC_ENTER);
        //labels
        loginText = new JLabel();
        loginText.setText("Username:");
        passwordText = new JLabel();
        passwordText.setText("Password:");
        //
        loginForm.add(loginText);
        loginForm.add(jLogin);
        loginForm.add(passwordText);
        loginForm.add(jPassword);
        loginForm.add(bEnter);
        loginForm.add(bRegister);
        setVisible(true);
    }

    public void resetForm() {
        jLogin.setText("");
        jPassword.setText("");
    }
    public JTextField getjLogin() {
        return jLogin;
    }
    public void setjLogin(JTextField jLogin) {
        this.jLogin = jLogin;
    }
    public JTextField getjPassword() {
        return jPassword;
    }
    public void setjPassword(JTextField jPassword) {
        this.jPassword = jPassword;
    }
    public JButton getbEnter() {
        return bEnter;
    }
    public void setbEnter(JButton bEnter) {
        this.bEnter = bEnter;
    }

}

1 answer

0


The problem is in its package hierarchy, by default Spring Boot looks below the hierarchy from where the main method is:

  • Your main is in the package com.market.app
  • Your controller is in the package com.market.controller

I suggest you organize your classes below com.market.app as also indicated in documentation, your controller package will then be com.market.app.controller.

Or you can indicate your packages using the annotation of @ComponentScan indicating the package com.market, but as I told you, the best option is to use the structure presented in the documentation, even for future maintenance.

  • thanks a lot bro I had remade and worked, but I had no idea the reason, now it worked exactly on account of my main is in the first package.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.