spring boot --- 初级体验
Spring boot的介绍我就不多说了,网上可以自己看一下。
它的优点就是:快!适合小白,没有复杂的配置文件。
缺点也很明显:坑有些多, 文档略少,报错有时不知道该如何处理。
下面做个最简单的入门:
首先使用maven来创建一个项目,jar即可。
假设工程名字为spring-boot
然后在pom.xml中添加如下配置:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.BUILD-SNAPSHOT</version>
</parent> <!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <!-- Package as an executable JAR -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <!-- Allow access to Spring milestones and snapshots -->
<!-- (you don't need this if you are using anything after 0.5.0.RELEASE) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
之后我们创建一个controller:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@EnableAutoConfiguration
public class SimpleController { @RequestMapping(value ="/hello", method = RequestMethod.GET)
@ResponseBody
public String hello(){
return "hello world";
} public static void main(String[] args) {
SpringApplication.run(SimpleController.class, args);
}
}
这个controller里有个main方法,看到没,我们直接运行即可,他会自动部署到tomcat或者jetty(8080端口不要被占用)。
然后在控制台你会看到:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.0.2.RELEASE) 2014-04-26 22:54:40.985 INFO 7236 --- [ main] c.r.spring.boot.SimpleController : Starting SimpleController on rollen with PID 7236 (D:\workspace\GitHub\SpringDemo\spring-boot\target\classes started by wenchao.ren in D:\workspace\GitHub\SpringDemo\spring-boot)
2014-04-26 22:54:41.008 INFO 7236 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Sat Apr 26 22:54:41 CST 2014]; root of context hierarchy
2014-04-26 22:54:41.583 INFO 7236 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-04-26 22:54:41.706 INFO 7236 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2014-04-26 22:54:41.706 INFO 7236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-04-26 22:54:41.785 INFO 7236 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2014-04-26 22:54:41.785 INFO 7236 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 779 ms
2014-04-26 22:54:42.055 INFO 7236 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2014-04-26 22:54:42.057 INFO 7236 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-04-26 22:54:42.289 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.368 INFO 7236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.rollenholt.spring.boot.SimpleController.hello()
2014-04-26 22:54:42.376 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.377 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.447 INFO 7236 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-04-26 22:54:42.459 INFO 7236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-04-26 22:54:42.460 INFO 7236 --- [ main] c.r.spring.boot.SimpleController : Started SimpleController in 1.675 seconds (JVM running for 1.944)
2014-04-26 22:54:54.963 INFO 7236 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-04-26 22:54:54.963 INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2014-04-26 22:54:54.971 INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 8 ms
当然还有一种启动运行方式,就是通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean
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);
}
}
启动完之后浏览器访问一下:http://localhost:8080/hello
就是这么快。
对于想学习/体验Spring的新手,快速建立项目模型等可以考虑用这种方式。
spring boot --- 初级体验的更多相关文章
- Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用[z]
前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...
- Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用
前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...
- 优质分享 | Spring Boot 入门到放弃!!!
持续原创输出,点击上方蓝字关注我 目录 前言 视频目录 如何获取? 总结 前言 最近不知不觉写Spring Boot专栏已经写了九篇文章了,从最底层的项目搭建到源码解析以及高级整合的部分,作者一直在精 ...
- spring boot(二):web综合开发
上篇文章介绍了Spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...
- Spring boot 提高篇
Spring boot 提高篇 上篇文章介绍了Spring boot初级教程:构建微服务:Spring boot 入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续 ...
- Spring Boot 学习(1)
文 by / 林本托 Tip 做一个终身学习的人. Spring Boot 初体验 Spring Boot 包含了很多 start(Spring boot 中 的叫法,就是一个模块,后文统一称模块,便 ...
- (转)Spring Boot(二):Web 综合开发
http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html 上篇文章介绍了 Spring Boot 初级教程:Spring ...
- spring boot(二)web综合开发
上篇文章介绍了Spring boot初级教程:spring boot(一):入门,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它特 ...
- Spring Boot 揭秘与实战(一) 快速上手
文章目录 1. 简介 1.1. 什么是Spring Boot 1.2. 为什么选择Spring Boot 2. 相关知识 2.1. Spring Boot的spring-boot-starter 2. ...
随机推荐
- C#中ListView易错的方法
现在有一个ListView(lv1),有2列. ListViewItem lvi = new ListViewItem(); lvi.Text = "语文"; lvi.SubIte ...
- Laravel Session 遇到的坑
这两天遇到了一个很奇怪的问题,更新session ,session的值不变.经过一番追查,终于找到问题,并搞明白了原理.写这篇博客记录下. 框架版本 Laravel 5.4 问题 先来描述下问题,我在 ...
- 取消a标签的页面跳转
如果代码是:<a href="#" class="juan-btn" onclick="showResult()">提交< ...
- 实现QQ、微信、新浪微博和百度第三方登录(Android Studio)
前言: 对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于第三方登录的实现,并拿出来与大家一同分享: 各大开放平台注册账户获取AppK ...
- Wireless Intranet Captive Portal
Wireless Intranet Captive Portal 配置AAA服务Radius认证 radius-server key 0 radius radius-server authentica ...
- css定位流布局
上面我们一起研究了浮动布局的特点和如何清除浮动给布局带来的不良影响,今天我们继续来研究定位流布局的特点和一些常用的布局技巧. 定位流主要有三种,一是相对定位,二是绝对定位,三是固定定位:下面我们一一进 ...
- js距离现在时间计算
<script language="javascript"> var biryear = 2015; var birmonth = 12; var birday = 1 ...
- 第二章 Struts 2的应用
2.1 Struts 2的应用 2.1.1 使用步骤 1.创建web项目,添加jar包,创建helloWorld.jsp页面 2.创建HelloWorldAction ...
- MySQL最常用日期时间函数
日期和时间函数 可能的需求: 当前时间是多少.下个月的今天是星期几.统计截止到当前日期前 3 天的收入总和-- 上述需求就需要使用日期和时间函数来实现: MySQL服务器中的三种时区设置: ①系统时区 ...
- 在Ubuntu中使用JAVA与tomcat搭建web服务器
一:材料 1.操作系统:ubuntu16.04 2.JAVA: jdk1.8.0 3.Tomcat:tomcat 8 4.域名:zhuandshao.cn 二:过程 1.安装java 1)在官网下载j ...