Spring Boot Maven构建成功后,为何运行main方法却无法启动?

2026-06-10 14:161阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计631个文字,预计阅读时间需要3分钟。

Spring Boot Maven构建成功后,为何运行main方法却无法启动?

plaintext

1.创建一个Controller类

从官方网站复制粘贴的代码如下: +package com.springboot.controller; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.*;

1、代码就一个Controller,从官网复制过来的,如下

package com.springboot.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/index") @ResponseBody String home() { return "Hello World"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }

2、在项目上右键,maven build,输入 spring-boot:run,过几秒后控制台能看见success,也能看见Hello World,但是没有传说中的那个用字符拼拼出来的spring图案,而且localhost:8080/也打不开,于是我机智的在上面的SampleController类中右键->java Application,果真,出错了,还more than 18... 错误如下:

1 Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener

等等之类的,就是找不到类的error

3、我的解决办法

之前我的pom.xml:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent>

百度、搜狗、谷歌找了2个小时的方法,自己手动引入其他dependency等等都不行,但是更改了springboot的版本就好了,更改后如下:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> </parent>

4、最后在SampleController类中右键->java Application,终于再console中输出了:

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.7.RELEASE) 2017-11-03 16:17:14.954 INFO 6416 --- [ main] c.s.controller.SampleController : Starting SampleController on USER-20170626MT with PID 6416 (D:\j2ee_workspace\SpringTest\target\classes started by Administrator in D:\j2ee_workspace\SpringTest) 2017-11-03 16:17:14.956 INFO 6416 --- [ main] c.s.controller.SampleController : No active profile set, falling back to default profiles: default 2017-11-03 16:17:15.005 INFO 6416 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@411f53a0: startup date [Fri Nov 03 16:17:15 CST 2017]; root of context hierarchy 2017-11-03 16:17:16.688 INFO 6416 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (localhost:8080/也能看见我的Hello World

仔细观察了最后一句,应该还有一些问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

Spring Boot Maven构建成功后,为何运行main方法却无法启动?

本文共计631个文字,预计阅读时间需要3分钟。

Spring Boot Maven构建成功后,为何运行main方法却无法启动?

plaintext

1.创建一个Controller类

从官方网站复制粘贴的代码如下: +package com.springboot.controller; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.web.bind.annotation.*;

1、代码就一个Controller,从官网复制过来的,如下

package com.springboot.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/index") @ResponseBody String home() { return "Hello World"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } }

2、在项目上右键,maven build,输入 spring-boot:run,过几秒后控制台能看见success,也能看见Hello World,但是没有传说中的那个用字符拼拼出来的spring图案,而且localhost:8080/也打不开,于是我机智的在上面的SampleController类中右键->java Application,果真,出错了,还more than 18... 错误如下:

1 Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener

等等之类的,就是找不到类的error

3、我的解决办法

之前我的pom.xml:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent>

百度、搜狗、谷歌找了2个小时的方法,自己手动引入其他dependency等等都不行,但是更改了springboot的版本就好了,更改后如下:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> </parent>

4、最后在SampleController类中右键->java Application,终于再console中输出了:

. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.7.RELEASE) 2017-11-03 16:17:14.954 INFO 6416 --- [ main] c.s.controller.SampleController : Starting SampleController on USER-20170626MT with PID 6416 (D:\j2ee_workspace\SpringTest\target\classes started by Administrator in D:\j2ee_workspace\SpringTest) 2017-11-03 16:17:14.956 INFO 6416 --- [ main] c.s.controller.SampleController : No active profile set, falling back to default profiles: default 2017-11-03 16:17:15.005 INFO 6416 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@411f53a0: startup date [Fri Nov 03 16:17:15 CST 2017]; root of context hierarchy 2017-11-03 16:17:16.688 INFO 6416 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (localhost:8080/也能看见我的Hello World

仔细观察了最后一句,应该还有一些问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

Spring Boot Maven构建成功后,为何运行main方法却无法启动?