原文地址:http://qindongliang.iteye.com/blog/2205633html
最近數據分析的項目也即將告一段落了,中間也積累了不少知識,特此記錄一下。其中用的最爽的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依賴:
java
- <?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>
核心代碼: web
- 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;
-
- @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);
-
- }
-
- }
控制檯輸出: spring
- 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.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等,支持不少好處,不言而喻!
express