Spring Boot开发之明月千城(一)
原文地址:http://qindongliang.iteye.com/blog/2205633
最近数据分析的项目也即将告一段落了,中间也积累了很多知识,特此记录一下。其中用的最爽的Web组合开发就是Intellij IDEA + Maven + Spring Boot + Velocity + Boostrap + jQuery了,关于后端的数据分析和处理的Hadoop模块,会放在Hadoop分类的博客文章中记录。

Spring Boot提供了一个强大的一键式Spring的集成开发环境,能够单独进行一个Spring应用的开发,其中:
(1)集中式配置(application.properties)+注解,大大简化了开发流程
(2)内嵌的Tomcat和Jetty容器,可直接打成jar包启动,无需提供Java war包以及繁琐的Web配置
(3)提供了Spring各个插件的基于Maven的pom模板配置,开箱即用,便利无比。
(4)可以在任何你想自动化配置的地方,实现可能
(5)提供更多的企业级开发特性,如何系统监控,健康诊断,权限控制
(6) 无冗余代码生成和XML强制配置
(7)提供支持强大的Restfult风格的编码,非常简洁
当然Spring Boot提供的功能,远远比上面的强大,散仙会在后续文章中,逐渐以实际工作中的项目为背景,穿插记录使用Spring Boot的心得体会。
下面看一个入门级的例子:
pom依赖:
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.spring.boot</groupId>
- <artifactId>springboot</artifactId>
- <version>1.0-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.2.3.RELEASE</version>
- </parent>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- </project>
核心代码:
- package controller;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- /**
- * Created by 三劫散仙 on 2015/4/24.
- */
- @Controller
- @EnableAutoConfiguration
- public class HellowController {
- @RequestMapping("/hellow")
- @ResponseBody
- public String hellow(){
- return "哈喽,Spring Boot !";
- }
- public static void main(String[] args) {
- //第一个简单的应用,
- SpringApplication.run(HellowController.class,args);
- }
- }
控制台输出:
- D:\soft\Java\jdk1.7.0_04\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:\idea\ideainstall\IntelliJ IDEA 14.0.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\soft\Java\jdk1.7.0_04\jre\lib\charsets.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\deploy.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\javaws.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\jce.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\jfr.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\jsse.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\management-agent.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\plugin.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\resources.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\rt.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\ext\dnsns.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\ext\localedata.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\ext\sunec.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\ext\sunjce_provider.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\ext\sunmscapi.jar;D:\soft\Java\jdk1.7.0_04\jre\lib\ext\zipfs.jar;D:\idea\ideaworkspace\Springboot\target\classes;C:\Users\qin\.m2\repository\org\springframework\boot\spring-boot-starter-web\1.2.3.RELEASE\spring-boot-starter-web-1.2.3.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\boot\spring-boot-starter\1.2.3.RELEASE\spring-boot-starter-1.2.3.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\boot\spring-boot\1.2.3.RELEASE\spring-boot-1.2.3.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.2.3.RELEASE\spring-boot-autoconfigure-1.2.3.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.2.3.RELEASE\spring-boot-starter-logging-1.2.3.RELEASE.jar;C:\Users\qin\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.11\jcl-over-slf4j-1.7.11.jar;C:\Users\qin\.m2\repository\org\slf4j\slf4j-api\1.7.11\slf4j-api-1.7.11.jar;C:\Users\qin\.m2\repository\org\slf4j\jul-to-slf4j\1.7.11\jul-to-slf4j-1.7.11.jar;C:\Users\qin\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.11\log4j-over-slf4j-1.7.11.jar;C:\Users\qin\.m2\repository\ch\qos\logback\logback-classic\1.1.3\logback-classic-1.1.3.jar;C:\Users\qin\.m2\repository\ch\qos\logback\logback-core\1.1.3\logback-core-1.1.3.jar;C:\Users\qin\.m2\repository\org\yaml\snakeyaml\1.14\snakeyaml-1.14.jar;C:\Users\qin\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\1.2.3.RELEASE\spring-boot-starter-tomcat-1.2.3.RELEASE.jar;C:\Users\qin\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.0.20\tomcat-embed-core-8.0.20.jar;C:\Users\qin\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.0.20\tomcat-embed-el-8.0.20.jar;C:\Users\qin\.m2\repository\org\apache\tomcat\embed\tomcat-embed-logging-juli\8.0.20\tomcat-embed-logging-juli-8.0.20.jar;C:\Users\qin\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.0.20\tomcat-embed-websocket-8.0.20.jar;C:\Users\qin\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.4.5\jackson-databind-2.4.5.jar;C:\Users\qin\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.4.5\jackson-annotations-2.4.5.jar;C:\Users\qin\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.4.5\jackson-core-2.4.5.jar;C:\Users\qin\.m2\repository\org\hibernate\hibernate-validator\5.1.3.Final\hibernate-validator-5.1.3.Final.jar;C:\Users\qin\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\qin\.m2\repository\org\jboss\logging\jboss-logging\3.1.3.GA\jboss-logging-3.1.3.GA.jar;C:\Users\qin\.m2\repository\com\fasterxml\classmate\1.0.0\classmate-1.0.0.jar;C:\Users\qin\.m2\repository\org\springframework\spring-core\4.1.6.RELEASE\spring-core-4.1.6.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\spring-web\4.1.6.RELEASE\spring-web-4.1.6.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\spring-aop\4.1.6.RELEASE\spring-aop-4.1.6.RELEASE.jar;C:\Users\qin\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;C:\Users\qin\.m2\repository\org\springframework\spring-beans\4.1.6.RELEASE\spring-beans-4.1.6.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\spring-context\4.1.6.RELEASE\spring-context-4.1.6.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\spring-webmvc\4.1.6.RELEASE\spring-webmvc-4.1.6.RELEASE.jar;C:\Users\qin\.m2\repository\org\springframework\spring-expression\4.1.6.RELEASE\spring-expression-4.1.6.RELEASE.jar;D:\idea\ideainstall\IntelliJ IDEA 14.0.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain controller.HellowController
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v1.2.3.RELEASE)
- 2015-04-24 01:12:41.399 INFO 4428 --- [ main] controller.HellowController : Starting HellowController on qin-PC with PID 4428 (D:\idea\ideaworkspace\Springboot\target\classes started by qin in D:\idea\ideaworkspace\Springboot)
- 2015-04-24 01:12:41.458 INFO 4428 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2923a47c: startup date [Fri Apr 24 01:12:41 CST 2015]; root of context hierarchy
- 2015-04-24 01:12:42.393 INFO 4428 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
- 2015-04-24 01:12:44.399 INFO 4428 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
- 2015-04-24 01:12:44.959 INFO 4428 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
- 2015-04-24 01:12:44.973 INFO 4428 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.20
- 2015-04-24 01:12:45.332 INFO 4428 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
- 2015-04-24 01:12:45.332 INFO 4428 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3880 ms
- 2015-04-24 01:12:46.274 INFO 4428 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
- 2015-04-24 01:12:46.295 INFO 4428 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
- 2015-04-24 01:12:46.296 INFO 4428 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
- 2015-04-24 01:12:46.481 INFO 4428 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2923a47c: startup date [Fri Apr 24 01:12:41 CST 2015]; root of context hierarchy
- 2015-04-24 01:12:46.542 INFO 4428 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hellow],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String controller.HellowController.hellow()
- 2015-04-24 01:12:46.544 INFO 4428 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 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)
- 2015-04-24 01:12:46.545 INFO 4428 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
- 2015-04-24 01:12:46.595 INFO 4428 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
- 2015-04-24 01:12:46.596 INFO 4428 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
- 2015-04-24 01:12:46.639 INFO 4428 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
- 2015-04-24 01:12:46.708 INFO 4428 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
- 2015-04-24 01:12:46.829 INFO 4428 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
- 2015-04-24 01:12:46.830 INFO 4428 --- [ main] controller.HellowController : Started HellowController in 5.978 seconds (JVM running for 6.739)
- 2015-04-24 01:13:29.470 INFO 4428 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
- 2015-04-24 01:13:29.470 INFO 4428 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
- 2015-04-24 01:13:29.486 INFO 4428 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
默认的内嵌容器,为tomcat,当然这个我们可以随便指定,包括端口号,http访问:

ok,至此,我们一个简单的http应用就开发完毕了,给人的感觉就是简直比用Python的Django和PHP还轻,而且还是我们十分熟悉的JAVA开发,所以与很多其他的JAVA开源项目相结合非常容易,比如Apache Lucene,Solr,Hadoop,Spark,ElasticSearch等,支持很多好处,不言而喻!
Spring Boot开发之明月千城(一)的更多相关文章
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
上篇博客使用Spring Boot开发Web项目我们简单介绍了使用如何使用Spring Boot创建一个使用了Thymeleaf模板引擎的Web项目,当然这还远远不够.今天我们再来看看如何给我们的We ...
- 使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用
在使用Spring boot 开发restful 风格的项目,put.delete方法不起作用,解决办法. 实体类Student @Data public class Student { privat ...
- Spring Boot 开发系列一 开发环境的一些九九
从今天开始写这个Spring Boot 开发系列,我是第二周学习JAVA的,公司号称springboot把JAVA的开发提升到填空的能力,本人是NET转JAVA的,想看看这个填空的东西到底有多强.废话 ...
- Spring Boot开发HTTPS协议的REST接口
Spring Boot开发HTTP的REST接口流程在前文中已经描述过,见<SpringBoot开发REST接口>. 如需要支持HTTPS,只需要在如上基础上进行设置.修改/resourc ...
- Spring Boot 开发微信公众号后台
Hello 各位小伙伴,松哥今天要和大家聊一个有意思的话题,就是使用 Spring Boot 开发微信公众号后台. 很多小伙伴可能注意到松哥的个人网站(http://www.javaboy.org)前 ...
- 天天玩微信,Spring Boot 开发私有即时通信系统了解一下
1/ 概述 利用Spring Boot作为基础框架,Spring Security作为安全框架,WebSocket作为通信框架,实现点对点聊天和群聊天. 2/ 所需依赖 Spring Boot 版本 ...
- spring boot开发,jar包一个一个来启动太麻烦了,写一个bat文件一键启动
spring boot开发,jar包一个一个来启动太麻烦了,写一个bat文件一键启动 @echo offcd D:\workProject\bushustart cmd /c "title ...
- spring boot 开发环境搭建(Eclipse)
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Boot 开发集成 WebSocket,实现私有即时通信系统
1/ 概述 利用Spring Boot作为基础框架,Spring Security作为安全框架,WebSocket作为通信框架,实现点对点聊天和群聊天. 2/ 所需依赖 Spring Boot 版本 ...
随机推荐
- php正则字符串提取汉字
/*$str 为输入.输出字符串变量*/ preg_match_all('/[\x{4e00}-\x{9fff}]+/u', $str, $matches); $str = join('', $mat ...
- asp.net防SQL/JS注入攻击:过滤标记
/// <summary>/// 过滤标记/// </summary>/// <param name="NoHTML">包括HTML,脚本,数据 ...
- 你会使用super()吗?你确定你了解它吗?
我们经常在类的继承当中使用super(), 来调用父类中的方法.例如下面: class A: def func(self): print('OldBoy') class B(A): def func( ...
- JavaScript深入解读
JavaScript是运行在浏览器上的脚本语言.我们平时看到丰富多彩的网页,这要在很大程度上归功于JavaScript. 引子 学点儿历史 JavaScript在编程语言的阵营里也是老资格了.它诞生于 ...
- Ibatis.Net 执行存储过程学习(八)
首先在数据库创建存储过程: create proc [dbo].[usp_GetPersonById] @Id int as begin select Id,Name from Person wher ...
- python之Anaconda版本管理
首先安装Anaconda,当其安装成功后,可以在cmd中测试是否安装成功,conda --version conda的环境管理 Conda的环境管理功能允许我们同时安装若干不同版本的Python,并能 ...
- 关于获取Windows下性能参数的总结
Windows下特定进程或者所有进程的CPU.物理内存.虚拟内存等性能参数的获取方法小结,包括如何在MFC中以及如何使用C#语言来获取参数. VC API:GlobalMemoryStatus 获取全 ...
- Spring Cloud微服务视频教程-百度云
Spring Cloud微服务视频教程-百度云 链接:https://pan.baidu.com/s/1mp8SkxNw7EfoTDtDKQMpIA 提取码: 关注公众号[GitHubCN]回复521 ...
- IE下css bug集合-翻译自haslayout.net
概述IE浏览器以不支持大量的css 属性出名,同时也因其支持的css属性中存在大量bug. 本页列举了IE下的一些问题,实例样本和一些我们已知的解决方法. 尽管我已经尽力按照它们本来的性质对它们进行分 ...
- JS跨域设置和取Cookie
在Javascript脚本里,一个cookie 实际就是一个字符串属性.当你读取cookie的值时,就得到一个字符串,里面当前WEB页使用的所有cookies的名称和值.每个cookie除了 name ...