Deniss M. Deniss M. - 2 months ago 21
Java Question

Error creating bean with name 'application', No default constructor found; nested exception is java.lang.NoSuchMethodException

I don't quite understand why this code gives me 'no default constructor found' error. The constructor is @Autowired. Everything seems to be injected correctly. Can anybody please help? Thanks

@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();
};
}
}


Edit: 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();
};
}
}

Answer

You can't autowire into main Spring Boot class. You can inject dependencies needed for CommandLineRunner as parameters of method annotated with @Bean and of course remove constructor injection for 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,
                       MessagingService messagingService,
                       Parser parser) {
        return args -> {
            Locale defaultLocale = Locale.getDefault();
            Locale.setDefault(defaultLocale);
            log.info("Using MessagingService: " + messagingService.getMyMessageCode());

            parser.parse();
        };
    }
}

EDIT: Correct context configuration after edit:

@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();
        };
    }
}