spring boot 一键开发spring应用
简介:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
You can use Spring Boot to create Java applications that can be started using java -jar or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.
Our primary goals are:
- **Provide a radically faster and widely accessible getting started experience for all Spring development.
- **Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
- **Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).
- **Absolutely no code generation and no requirement for XML configuration.
以上是spring boot官方文档的介绍;大体意思是说spring boot很吊,很牛x;好吧,屌丝级的英文水平就不在此显摆了。
使用spring boot需要具备的环境:
* Java 6+.
* Maven (版本尽量使用新的,有个版本号最低限制,新版的文档没有找到。)
* IntelliJ IDEA(或者其他IDE)
请启动大脑的最低功耗完成以上软件的安装。接着你就可以so so so的开发spring应用了。
start:
1、新建maven工程;
2、添加pom依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、写main方法:
package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
4、启动main方法,控制台见到如下日志,表示ok!
......
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.0.RELEASE)
2016-11-12 10:58:15.625 INFO 9896 --- [ main] App : Starting App on DESKTOP-M3PLLG6 with PID 9896 (E:\ideaProject\commerce-platform-back\sboot\target\classes started by Yan Meng in E:\ideaProject\commerce-platform-back\sboot)
2016-11-12 10:58:15.640 INFO 9896 --- [ main] App : No active profile set, falling back to default profiles: default
2016-11-12 10:58:15.812 INFO 9896 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6298e03e: startup date [Sat Nov 12 10:58:15 CST 2016]; root of context hierarchy
2016-11-12 10:58:19.814 INFO 9896 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-11-12 10:58:19.886 INFO 9896 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-11-12 10:58:19.888 INFO 9896 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-11-12 10:58:20.164 INFO 9896 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-11-12 10:58:20.164 INFO 9896 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4365 ms
2016-11-12 10:58:20.534 INFO 9896 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-11-12 10:58:20.541 INFO 9896 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-11-12 10:58:20.542 INFO 9896 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-11-12 10:58:20.543 INFO 9896 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-11-12 10:58:20.543 INFO 9896 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-11-12 10:58:21.248 INFO 9896 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6298e03e: startup date [Sat Nov 12 10:58:15 CST 2016]; root of context hierarchy
2016-11-12 10:58:21.388 INFO 9896 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String App.home()
2016-11-12 10:58:21.394 INFO 9896 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-11-12 10:58:21.395 INFO 9896 --- [ 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)
2016-11-12 10:58:21.470 INFO 9896 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-12 10:58:21.470 INFO 9896 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-12 10:58:21.559 INFO 9896 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-12 10:58:22.043 INFO 9896 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-11-12 10:58:22.157 INFO 9896 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-11-12 10:58:22.187 INFO 9896 --- [ main] App : Started App in 8.602 seconds (JVM running for 10.204)
访问本地 http://localhost:8080/ 即可看到结果啦!
ok,后续补充使用spring boot 在项目开发中的实战细节。