做Java Web的同學,都知道項目啟動需要放到servlet容器里面運行,無論是使用哪一款IDE,都是非常麻煩的一件事情。在很早之前,一個servlet容器下可以放下很多的項目,並一起運行,而到現在這個年代,很多服務一台機子都不夠用了。所以很多時候,一個容器本來就只為一個項目服務,這樣一來,容器式的服務器,還需要打包重啟server這樣的行為,看起來就非常繁瑣,而且不利於持續集成。
Spring Boot是一個可以讓項目使用極少的配置,並且不需要額外配置到外部的容器中,就可以快速啟動項目的微服務框架。看看它是怎么解放我們的生產力的。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.1.RELEASE)
添加配置到POM.XML
目前 Spring Boot的最新版本是 1.3.1.RELEASE
,這里我是使用maven作為項目的管理方案,當然你也可以使用gradle來作為項目管理方案添加Spring Boot到項目中。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
編寫代碼
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 SampleApplication {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleApplication.class, args);
}
}
然后直接執行這個類的main()
方法,console里面就會打印出以下的信息。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.1.RELEASE)
......
2016-01-03 20:40:15.732 INFO 12899 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f021e6c: startup date [Sun Jan 03 20:40:15 CST 2016]; root of context hierarchy
2016-01-03 20:40:17.909 INFO 12899 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: 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]]
2016-01-03 20:40:19.232 INFO 12899 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-03 20:40:19.256 INFO 12899 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
......
這個時候,一個簡單的web項目就啟動完畢了。
如果需要修改項目啟動的配置『比如服務的端口,最大支持線程數量等等』,可以在resource目錄下面新建一個application.properties,里面去填需要的配置。具體的參數可以參考官方的手冊。
效果演示
我們在命令訪問這個127.0.0.1:8080
這個地址。
➜ ~ http get 127.0.0.1:8080
HTTP/1.1 200 OK
Content-Length: 12
Content-Type: text/plain;charset=UTF-8
Date: Sun, 03 Jan 2016 12:52:03 GMT
Server: Apache-Coyote/1.1
Hello World!
可以看到項目已經成功啟動,並且可以訪問了。
其實是還是容器
從console信息中不難發現,Spring Boot其實還是使用了tomcat,只不過tomcat變成了嵌入式的服務器了,當然我們也可以通過bean管理,把默認的tomcat方案換成其他的方式。
寫在后面
過去我們寫一些web小項目,是一個非常繁瑣的過程,調試不方便,反復地啟動tomcat,單元測試的內容和容器高度耦合,配置巨多,Spring Boot雖然不是什么銀彈,但是很好地解決了一些問題,讓開發者可以更加專注業務的開發,減少配置上時間的消耗。