【问题标题】:Error creating bean with name 'application', No default constructor found; nested exception is java.lang.NoSuchMethodException创建名为“应用程序”的 bean 时出错,未找到默认构造函数;嵌套异常是 java.lang.NoSuchMethodException
【发布时间】:2016-09-11 08:52:38
【问题描述】:

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

【问题讨论】:

  • 默认构造函数是指没有任何参数的构造函数。如果您有任何默认构造函数,请查看您的代码。不要在主类中使用带有@SpringBootAnnotation 注释的其他spring 注释。

标签: java spring dependency-injection spring-boot spring-ioc


【解决方案1】:

luboskrnac 的回答是正确的。

但是如果你真的想使用Constructor Injection你可以升级你的SpringBoot版本到1.4.0.RELEASE 将使用 Spring 4.3.2.RELEASE

Spring 4.3 构造函数注入 支持 @Configuration

New Features and Enhancements in Spring Framework 4.3

【讨论】:

  • 谢谢!现在我明白了为什么构造函数注入在工作中几乎无处不在,但在家里却没有。我在工作中有 1.4.0 版的 spring boot,但在家里有 1.2.3 版。感谢您的提示!
【解决方案2】:

您不能自动装配到主 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();
        };
    }
}

【讨论】:

  • 谢谢!这就是我想知道的。我已经对我的课程进行了编辑,请您看看并告诉我是否可以以我认为更干净的方式注入我的依赖项?
  • 我为您的课程更新课程添加了正确的上下文配置。
  • 非常感谢!因此,可以根据需要将尽可能多的依赖项注入到我的 CommandLineRunner 应用程序中。我问的原因是我以前做过这个并且被告知,这是不正确的,应该按照我的 EDIT 版本的课程来完成。
  • 通过@Bean方法参数注入是Spring IoC容器完全有效和支持的特性。如果有人说“不正确”,他应该自学。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多