Error creating bean named ‘application’, unable to find default constructor; The nested exception is Java lang.NoSuchMethodException

I don't quite understand why this code gives me the error "can't find the default constructor" The constructor is @ Autowired Everything seems to be right Can I help you? thank you

@SpringBootApplication
public class Application {

    private ApplicationContext applicationContext;
    private MessagingService messagingService;
    private Parser parser;

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    @Autowired
    public Application(ApplicationContext applicationContext,MessagingService messagingService,Parser parser)
    {
        this.applicationContext = applicationContext;
        Assert.notNull(messagingService,"MessagingService must not be null");
        this.messagingService = messagingService;
        Assert.notNull(parser,"Parser must not be null");
        this.parser = parser;
    }

    public static void main (String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public CommandLineRunner app() {
        return args -> {
            Locale defaultLocale = Locale.getDefault();
            Locale.setDefault(defaultLocale);
            log.info("Using MessagingService: " + messagingService.getMyMessageCode());

            parser.parse();
        };
    }
}

Editing: updated application class

@SpringBootApplication
public class Application {

    @Autowired
    private MessagingService messagingService;
    @Autowired
    private Parser parser;

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public Application() {}

    public static void main (String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public CommandLineRunner app() {
        return args -> {
            Locale defaultLocale = Locale.getDefault();
            Locale.setDefault(defaultLocale);
            log.info("Using MessagingService: " + messagingService.getMyMessageCode());

            parser.parse();
        };
    }
}

Solution

You cannot automatically load the main spring boot class You can take the dependency injection required by commandlinerunner as the parameter of the method annotated with @ bean, and of course, delete the constructor injection of the main class:

@SpringBootApplication
public class Application {
    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main (String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public CommandLineRunner app(ApplicationContext applicationContext,Parser parser) {
        return args -> {
            Locale defaultLocale = Locale.getDefault();
            Locale.setDefault(defaultLocale);
            log.info("Using MessagingService: " + messagingService.getMyMessageCode());

            parser.parse();
        };
    }
}

Edit: correct context configuration after editing:

@SpringBootApplication
public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public Application() {}

    public static void main (String[] args) {
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public CommandLineRunner app(MessagingService messagingService,Parser parser) {
        return args -> {
            Locale defaultLocale = Locale.getDefault();
            Locale.setDefault(defaultLocale);
            log.info("Using MessagingService: " + messagingService.getMyMessageCode());

            parser.parse();
        };
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>