经过多年的发展java的开发体系也越来越全面我们经历了从spring2.5、springMVC3X到现在的springboot,虽然开发方式发生了变化,唯一不变的是我们仍然需要坚持不懈的学习劲头和激情,不断努力不断提高,因此这次特地分享下最近学习springboot的经验,这里不会每个步骤都写的那么详细,就简明扼要的写一下
一 学习springboot需要具备的基本要求
1 对spring以及springmvc有使用经验和基础的调试能力
2 有jpa或者hibernate的使用经验
3 会使用freemarker
4 熟练使用mybatis
5 熟练使用maven
6 简单使用服务器中间件如tomcat,jboss,weblogic等
如果掌握了以上技术,springboot便能快速上手一学就会,因此springboot对程序员的综合能力还是很高的
二 下面介绍springboot的基本配置和application.properties
经过一段时间的学习我觉得springboot的特色是规约配置,也就是说springboot中规则大于配置,更多的我们需要记住里面的那些规则,如下图所示
看到这里不知道大家会不会和我一样对springboot的灵活性感到无比惊讶,关于配置文件里的众多配置大家可以查查资料,这里就不全部写出来了
三 项目创建后pom文件的写法
maven的使用就不说了,直接贴给大家一个常用的,如下所示
4.0.0
com.debug
usemybatis
0.0.1-SNAPSHOT
jar
usemybatis
http://maven.apache.org
UTF-8
1.8
org.springframework.boot
spring-boot-starter-parent
1.4.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
true
true
junit
junit
3.8.1
test
org.springframework.boot
spring-boot-maven-plugin
true
四 springboot的启动入口
默认情况下项目创建完毕后会生产一个叫App.java的文件,我们只需要在这个文件上加上注解以及 SpringApplication.run(App.class, args);就OK了,如下所示:
package com.debug;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SpringApplication.run(App.class, args);
}
}
Hello World!
.__ ___. __
_____ ___.__. ___________________|__| ____ ____\_ |__ ____ _____/ |_
/ < | | / ___/\____ \_ __ \ |/ \ / ___\| __ \ / _ \ / _ \ __\
| Y Y \___ | \___ \ | |_> > | \/ | | \/ /_/ > \_\ ( <_> | <_> ) |
|__|_| / ____| /____ >| __/|__| |__|___| /\___ /|___ /\____/ \____/|__|
\/\/ \/ |__| \//_____/ \/
2017-09-24 13:08:13.260 INFO 10632 --- [ main] com.debug.App : Starting App on cry with PID 10632 (G:\stsspace\usespringboot01\target\classes started by chenwill2 in G:\stsspace\usespringboot01)
2017-09-24 13:08:13.281 INFO 10632 --- [ main] com.debug.App : The following profiles are active: prod
2017-09-24 13:08:13.558 INFO 10632 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@635eaaf1: startup date [Sun Sep 24 13:08:13 CST 2017]; root of context hierarchy
2017-09-24 13:08:27.815 INFO 10632 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 80 (http)
2017-09-24 13:08:27.863 INFO 10632 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-09-24 13:08:27.864 INFO 10632 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-09-24 13:08:28.367 INFO 10632 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-09-24 13:08:28.368 INFO 10632 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 14831 ms
2017-09-24 13:08:28.613 INFO 10632 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-24 13:08:28.621 INFO 10632 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-24 13:08:28.678 INFO 10632 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-24 13:08:28.678 INFO 10632 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-24 13:08:28.678 INFO 10632 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-24 13:08:29.274 INFO 10632 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@635eaaf1: startup date [Sun Sep 24 13:08:13 CST 2017]; root of context hierarchy
2017-09-24 13:08:29.413 INFO 10632 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/sayHello.do]}" onto public java.lang.String com.debug.HelloController.sayHello()
2017-09-24 13:08:29.415 INFO 10632 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hi/sayHi.do]}" onto public java.lang.String com.debug.HiController.sayHi()
2017-09-24 13:08:29.420 INFO 10632 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity
2017-09-24 13:08:29.422 INFO 10632 --- [ 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)
2017-09-24 13:08:29.468 INFO 10632 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-24 13:08:29.468 INFO 10632 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-24 13:08:29.539 INFO 10632 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-24 13:08:29.802 INFO 10632 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-09-24 13:08:30.553 INFO 10632 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 80 (http)
2017-09-24 13:08:30.558 INFO 10632 --- [ main] com.debug.App : Started App in 19.18 seconds (JVM running for 21.845)
五 controller的写法大致如下
package com.debug;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello/sayHello.do")
public String sayHello() {
return "Hello Spring Boot!";
}
}
从浏览器输入url后运行效果如下
至此第一个springboot程序就写完了
备注:开发工具我使用的是spring tool suite, jdk是1.8的版本