做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虽然不是什么银弹,但是很好地解决了一些问题,让开发者可以更加专注业务的开发,减少配置上时间的消耗。

最新文章

  1. VS2015 使用及插件推荐
  2. ElasticSearch性能优化官方建议
  3. pyqt5-为QListWidget添加右键菜单
  4. 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts
  5. 再次用CodeIgniter实现简易blog
  6. android修改系统时区
  7. 移动WEB测试工具 Adobe Edge Inspect
  8. Java IO(四)
  9. WIN10主动推升级,有点意思
  10. UVa 10837 A Research Problem 欧拉函数
  11. &lt;转载&gt;模板声明中template &lt;typename T&gt;和template &lt;class T&gt;
  12. EntityFramework Core高并发深挖详解,一纸长文,你准备好了吗?
  13. angularJS directive详解(自定义指令)
  14. 《C程序猿从校园到职场》勘误
  15. 【Vbox】centos虚拟机安装usb网卡驱动
  16. 【爆料】-《西澳大学毕业证书》UWA一模一样原件
  17. Linux内存管理 (25)内存sysfs节点解读
  18. (C#)日志接口请求响应时间
  19. Web应用的统一异常处理(二十四)
  20. struts2 18拦截器详解(十)

热门文章

  1. C# 注册 Windows 热键
  2. Js 变量声明提升和函数声明提升
  3. .Net Core MVC 网站开发(Ninesky) 2.2、栏目管理功能-System区域添加
  4. Visual Studio 2012远程调试中遇到的问题
  5. kafka配置与使用实例
  6. Ajax实现原理,代码封装
  7. [内核笔记1]内核文件结构与缓存——inode和对应描述
  8. MSYS2环境下编译X265
  9. 使用po模式读取豆瓣读书最受关注的书籍,取出标题、评分、评论、题材 按评分从小到大排序并输出到txt文件中
  10. Html 制作相册