创建名为’application’的bean时出错,找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了创建名为’application’的bean时出错,找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2788字,纯文字阅读大概需要4分钟。
内容图文
![创建名为’application’的bean时出错,找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException](/upload/InfoBanner/zyjiaocheng/746/e17f3fad9f00479d825624e4e80f1596.jpg)
我不太明白为什么这段代码给我“找不到默认构造函数”错误.构造函数是@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();
};
}
}
内容总结
以上是互联网集市为您收集整理的创建名为’application’的bean时出错,找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException全部内容,希望文章能够帮你解决创建名为’application’的bean时出错,找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。