编程语言
首页 > 编程语言> > 创建名为’application’的bean时出错,找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException

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

作者:互联网

我不太明白为什么这段代码给我“找不到默认构造函数”错误.构造函数是@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();
        };
    }
}

标签:java,spring,spring-boot,dependency-injection,spring-ioc
来源: https://codeday.me/bug/20190727/1554011.html