Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建名为';应用程序';,未找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException_Java_Spring_Dependency Injection_Spring Boot_Spring Ioc - Fatal编程技术网

创建名为';应用程序';,未找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException

创建名为';应用程序';,未找到默认构造函数;嵌套异常是java.lang.NoSuchMethodException,java,spring,dependency-injection,spring-boot,spring-ioc,Java,Spring,Dependency Injection,Spring Boot,Spring Ioc,我不太明白为什么这段代码会给我“找不到默认构造函数”错误。构造函数是@Autowired。一切似乎都注入正确。有人能帮忙吗?谢谢 @SpringBootApplication public class Application { private ApplicationContext applicationContext; private MessagingService messagingService; private Parser parser; priv

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


您不能自动连接到主Spring引导类。您可以将
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();
        };
    }
}
我的回答是正确的

但是如果你真的想使用
构造函数注入
,你可以升级
SpringBoot
版本到
1.4.0。发布
它将使用
Spring 4.3.2。发布

开始,Spring 4.3构造函数注入在
@Configuration
类上受支持


默认构造函数是指没有任何参数的构造函数。如果有任何默认构造函数,请查看代码。不要在主类中使用其他spring注释和@SpringBootAnnotation注释。谢谢!这就是我想知道的。我已经对我的类进行了编辑,你能看一下并告诉我我是否可以以我认为更干净的方式注入依赖项吗?我为你的类更新类提供了正确的上下文配置。非常感谢!因此,在我的CommandLineRunner应用程序中注入所需的依赖项是可以的。我问的原因是,我以前做过,而且有人告诉我,这是不正确的,应该按照我的类编辑版本来做。通过
@Bean
方法参数进行注入是完全有效的,并且是Spring IoC容器支持的功能。如果有人说“这是不对的”,他应该教育自己。谢谢!现在我明白了为什么构造函数注入在工作中几乎无处不在,但在家里却不起作用。我的SpringBoot版本是1.4.0,但在家里是1.2.3。谢谢你的提示!
@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();
        };
    }
}