博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot快速入门
阅读量:5127 次
发布时间:2019-06-13

本文共 8873 字,大约阅读时间需要 29 分钟。

 

1.   设置spring boot的parent

org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE

 

说明:Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。

 

2.   导入spring boot的web支持

org.springframework.boot
spring-boot-starter-web

3.   添加Spring boot的插件

org.springframework.boot
spring-boot-maven-plugin

 

最终的pom.xml:

4.0.0
cn.qiaoliqiang
spring-boot
0.0.1-SNAPSHOT
war
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework
spring-webmvc
4.3.7.RELEASE
com.jolbox
bonecp-spring
0.8.0.RELEASE
${project.artifactId}
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-resources-plugin
UTF-8
org.apache.maven.plugins
maven-compiler-plugin
1.7
1.7
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2

4.   编写第一个Spring Boot的应用

@Controller@SpringBootApplication@Configurationpublic class HelloApplication {        @RequestMapping("hello")    @ResponseBody    public String hello(){        return "hello world!";    }        public static void main(String[] args) {        SpringApplication.run(HelloApplication.class, args);    }}

代码说明:

1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;

2、@Configuration:这是一个配置Spring的配置类;

3、@Controller:标明这是一个SpringMVC的Controller控制器;

4、main方法:在main方法中启动一个应用,即:这个应用的入口;

 

5.   启动应用

在Spring Boot项目中,启动的方式有两种,一种是直接run Java Application另外一种是通过Spring Boot的Maven插件运行。

第一种:

 

 结果:

.   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.5.2.RELEASE)2018-01-11 16:54:36.745  INFO 882716 --- [           main] cn.qlq.springboot.HelloApplication       : Starting HelloApplication on root with PID 882716 (E:\MavenEclipseWorkspace\spring-boot\target\classes started by liqiang in E:\MavenEclipseWorkspace\spring-boot)2018-01-11 16:54:36.750  INFO 882716 --- [           main] cn.qlq.springboot.HelloApplication       : No active profile set, falling back to default profiles: default2018-01-11 16:54:36.872  INFO 882716 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67dd2285: startup date [Thu Jan 11 16:54:36 CST 2018]; root of context hierarchy2018-01-11 16:54:40.517  INFO 882716 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2018-01-11 16:54:40.678  INFO 882716 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat2018-01-11 16:54:40.680  INFO 882716 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.112018-01-11 16:54:40.998  INFO 882716 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2018-01-11 16:54:40.999  INFO 882716 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4131 ms2018-01-11 16:54:41.365  INFO 882716 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]2018-01-11 16:54:41.372  INFO 882716 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]2018-01-11 16:54:41.373  INFO 882716 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-01-11 16:54:41.373  INFO 882716 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-01-11 16:54:41.373  INFO 882716 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]2018-01-11 16:54:42.097  INFO 882716 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@67dd2285: startup date [Thu Jan 11 16:54:36 CST 2018]; root of context hierarchy2018-01-11 16:54:42.240  INFO 882716 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String cn.qlq.springboot.HelloApplication.hello()2018-01-11 16:54:42.250  INFO 882716 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity
> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2018-01-11 16:54:42.251 INFO 882716 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-01-11 16:54:42.351 INFO 882716 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-11 16:54:42.351 INFO 882716 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-11 16:54:42.481 INFO 882716 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-11 16:54:43.241 INFO 882716 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-01-11 16:54:43.414 INFO 882716 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2018-01-11 16:54:43.429 INFO 882716 --- [ main] cn.qlq.springboot.HelloApplication : Started HelloApplication in 8.809 seconds (JVM running for 9.52)2018-01-11 17:04:05.722 INFO 882716 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2018-01-11 17:04:05.722 INFO 882716 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2018-01-11 17:04:05.804 INFO 882716 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 82 ms

  

访问:

 

 

第二种:以spring-boot插件的方式运行:

(1)添加一个maven常用命令

 

 

 

(2)运行上面建的maven命令:

 

 

 

 

 

 

结果与上面一样

 

看到如下信息就说明启动成功了:

2018-01-11 18:01:42.242  INFO 1021040 --- [           main] cn.qlq.springboot.HelloApplication       : Started HelloApplication in 6.845 seconds (JVM running for 16.07)

 

测试:

 

转载于:https://www.cnblogs.com/qlqwjy/p/8269891.html

你可能感兴趣的文章
There is no PasswordEncoder mapped for the id "null"
查看>>
windows10 conda python多版本切换
查看>>
Linux配置日志服务器
查看>>
P6 EPPM 16.1 安装和配置指南 1
查看>>
C语言:九九乘法表打印
查看>>
Java_Activiti5_菜鸟也来学Activiti5工作流_之JUnit单元测试(四)
查看>>
codeforce626D (概率)
查看>>
HD1385Minimum Transport Cost(Floyd + 输出路径)
查看>>
Ajax技术
查看>>
MVC解决方案发布IIS 登录页面需要输入两次帐号问题
查看>>
Visual Studio 2017 初次体验
查看>>
zTree树
查看>>
tips 前端 点击事件
查看>>
ACM: 限时训练题解-Epic Professor-水题
查看>>
Mybatis的使用
查看>>
Node.js 连接 MySQL
查看>>
ACM-ICPC 2018 world final A题 Catch the Plane
查看>>
那些年,那些书
查看>>
如何进行库存管理?
查看>>
面向对象六大基本原则的理解
查看>>