終於來到鐵人賽最後一天了,參加鐵人賽果的這30天,有些主題很順,有些則debug老半天,上StackOverFlow,google找跑不出來的原因,幾度感到挫敗也曾想中斷,後來還是堅持到最後了,到了今天,自己也感覺到對自己選的這個主題有比較熟悉,也知道哪裡自己觀念還需要加強,另外也了解到還須要學些什麼,例如Spring Data,再來是前端框架部分,因為沒學過Angular JS而且對javascript不熟,前後端的data binding不知道如何作,AJAX的部分也就沒有分享到,鐵人賽後應該會開始學Angular JS,但最新消息是Angular JS要出2.0,雖然還再development階段,網路消息Angular JS 1.x幾乎是砍掉重練,現在我也猶豫是不是先學其他的前端框架如ember.js或是knockout.js來實踐AJAX。
Anyway,透過這三十天,不難發現其實處理Dependency及相關Config挺瑣碎的,如果熟練是還好啦,但人總是懶惰的啦,為簡化上述兩者,Spring Boot專案包裝好Spring各個專案的相關dependency,以及不建議再使用XML,改用Java Config的方式來設定Spring,新增專案時將沒有XML檔,未來應該會朝向使用Spring Boot來開發Spring應用程式,今年度的鐵人賽就以Hello! Spring Boot!來作個結束吧!
要新增Spring Boot專案,可以透過STS新增Spring Starter Project,點選後填入資訊如下,Style的部分請先勾選Web即可,若勾選JDBC或是JPA等會要設定資料庫:
打開Maven Dependency,可以發現Spring Boot已經將相關的dependency下載好了,包括Jackson databind(JSON)、hibernate validator、logging相關等,另可知Spring Boot 1.1.8對應的Springframework是4.0.7版
打開pom.xml,可以發現dependencies簡潔多了(僅52行,前29天的分享pom.xml接近280行),而且未來Spring Boot新版可以在Parent標籤裡鍵入新版次,即可更新相關Dependency
<?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>tw.blogger.springtech</groupId>
<artifactId>boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>HelloSpringBoot</name>
<description>Hello! Spring Boot!</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>tw.blogger.springtech.boot.Application</start-class>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
專案新增好後只有兩個java檔:Application.java及ServletInitializer.java,重點還是Application.java,新增了@EnableAutoConfiguration,Spring號稱會依據你的dependency進行設定,但官方文件對此並未說明太多,只說明會依據dependency來判斷專案的所需的設定,另外StackOverFlow上也有人建議如果自己已經熟悉相關設定,則建議移除@EnableAutoConfiguration,避免與自己寫的configuration衝突。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
新增一個HelloBootController,其code如下:
package tw.blogger.springtech.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloBootController {
@RequestMapping("/")
public String helloPage(){
return "Hello! Spring Boot!";
}
}
專案上按右鍵->Run As->Spring Boot App,Console輸出如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.1.8.RELEASE)
2014-10-28 22:31:45.692 INFO 9424 --- [ main] tw.blogger.springtech.boot.Application : Starting Application on YiRenPC with PID 9424 (D:\java\HelloBoot\target\classes started by joombuopre in D:\java\HelloBoot)
2014-10-28 22:31:45.731 INFO 9424 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@c8e4bb0: startup date [Tue Oct 28 22:31:45 CST 2014]; root of context hierarchy
2014-10-28 22:31:46.227 INFO 9424 --- [ 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]]
2014-10-28 22:31:46.767 INFO 9424 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-10-28 22:31:46.867 INFO 9424 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-10-28 22:31:46.868 INFO 9424 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.55
2014-10-28 22:31:46.947 INFO 9424 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-10-28 22:31:46.947 INFO 9424 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1219 ms
2014-10-28 22:31:47.326 INFO 9424 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-10-28 22:31:47.328 INFO 9424 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-10-28 22:31:47.428 INFO 9424 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-10-28 22:31:47.534 INFO 9424 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String tw.blogger.springtech.boot.controller.HelloWorldController.helloPage()
2014-10-28 22:31:47.535 INFO 9424 --- [ 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)
2014-10-28 22:31:47.535 INFO 9424 --- [ 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)
2014-10-28 22:31:47.547 INFO 9424 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-10-28 22:31:47.548 INFO 9424 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-10-28 22:31:47.645 INFO 9424 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-10-28 22:31:47.694 INFO 9424 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-10-28 22:31:47.695 INFO 9424 --- [ main] tw.blogger.springtech.boot.Application : Started Application in 2.37 seconds (JVM running for 2.911)
2014-10-28 22:31:53.278 INFO 9424 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-10-28 22:31:53.279 INFO 9424 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2014-10-28 22:31:53.289 INFO 9424 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms
打開瀏覽器打入http://localhost:8080,即出現Hello! Spring Boot!
如果你不是用STS作為開發工具,你也可以上http://start.spring.io,來產生Maven或Gradle專案,再匯入IDE即可
明年IT鐵人賽再會!!!