创建名称为'application'的bean时出错,找不到默认的构造函数;嵌套的异常是java.lang.NoSuchMethodException

丹尼斯·M。

我不太明白为什么这段代码会给我“未找到默认构造函数”错误。构造函数为@Autowired。一切似乎已正确注入。有人可以帮忙吗?谢谢

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

编辑:更新了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();
        };
    }
}
奥尔斯克纳克

您不能自动连接到主要的Spring Boot类。您可以注入所需的依赖项CommandLineRunner作为用注释的方法的参数@Bean,当然也可以删除主类的构造函数注入:

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

编辑:编辑后更正上下文配置:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档