简介 

相信很多人都接触spring框架很长时间了,每次搭建spring框架的时候都需要配置好多的jar、xml,做很多繁琐重复的配置,稍微不留神就会出现各种各样的问题,每次调试真的是香菇、蓝瘦啊。

spring boot的出现帮助我们彻底解决了这些jar的依赖,只需要很少的配置就可以完成我们的开发工作,我们可以把自己的应用打包成jar,使用java -jar来运行spring web应用,spring boot集成了很多的web容器,后面都会慢慢讲到这些,今天我们就开始使用spring boot完成一个最简单的web应用。

使用maven构建项目

spring官网提供了了一个非常方便的地址来帮助我们创建spring boot项目,http://start.spring.io/ ,输入相关参数点击 Generate Project 即可下载自动生成的压缩包。

该项目采用maven构建,加压后直接使用自己的IDE导入maven工程即可

1,打开pom文件就可以看到新的项目比传统的spring mvc项目的pom文件接口清晰了很多

  

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.4.1.RELEASE</version>
  5. </parent>

第一个地方是多了上面的代码,这个spring boot会自动帮我门引入一些约定的配置,查看源码得知

  1. <?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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-dependencies</artifactId>
  6. <version>1.4.1.RELEASE</version>
  7. <relativePath>../../spring-boot-dependencies</relativePath>
  8. </parent>
  9. <artifactId>spring-boot-starter-parent</artifactId>
  10. <packaging>pom</packaging>
  11. <name>Spring Boot Starter Parent</name>
  12. <description>Parent pom providing dependency and plugin management for applications
  13. built with Maven</description>
  14. <url>http://projects.spring.io/spring-boot/</url>
  15. <organization>
  16. <name>Pivotal Software, Inc.</name>
  17. <url>http://www.spring.io</url>
  18. </organization>
  19. <properties>
  20. <java.version>1.6</java.version>
  21. <resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <maven.compiler.source>${java.version}</maven.compiler.source>
  25. <maven.compiler.target>${java.version}</maven.compiler.target>
  26. </properties>
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-core</artifactId>
  32. <version>${spring.version}</version>
  33. <exclusions>
  34. <exclusion>
  35. <groupId>commons-logging</groupId>
  36. <artifactId>commons-logging</artifactId>
  37. </exclusion>
  38. </exclusions>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <!-- Turn on filtering by default for application properties -->
  44. <resources>
  45. <resource>
  46. <directory>${basedir}/src/main/resources</directory>
  47. <filtering>true</filtering>
  48. <includes>
  49. <include>**/application*.yml</include>
  50. <include>**/application*.properties</include>
  51. </includes>
  52. </resource>
  53. <resource>
  54. <directory>${basedir}/src/main/resources</directory>
  55. <excludes>
  56. <exclude>**/application*.yml</exclude>
  57. <exclude>**/application*.properties</exclude>
  58. </excludes>
  59. </resource>
  60. </resources>
  61. <pluginManagement>
  62. <plugins>
  63. <!-- Apply more sensible defaults for user projects -->
  64. <plugin>
  65. <groupId>org.apache.maven.plugins</groupId>
  66. <artifactId>maven-failsafe-plugin</artifactId>
  67. <executions>
  68. <execution>
  69. <goals>
  70. <goal>integration-test</goal>
  71. <goal>verify</goal>
  72. </goals>
  73. </execution>
  74. </executions>
  75. </plugin>
  76. <plugin>
  77. <groupId>org.apache.maven.plugins</groupId>
  78. <artifactId>maven-jar-plugin</artifactId>
  79. <configuration>
  80. <archive>
  81. <manifest>
  82. <mainClass>${start-class}</mainClass>
  83. <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
  84. </manifest>
  85. </archive>
  86. </configuration>
  87. </plugin>
  88. <plugin>
  89. <groupId>org.apache.maven.plugins</groupId>
  90. <artifactId>maven-surefire-plugin</artifactId>
  91. <configuration>
  92. <includes>
  93. <include>**/*Tests.java</include>
  94. <include>**/*Test.java</include>
  95. </includes>
  96. <excludes>
  97. <exclude>**/Abstract*.java</exclude>
  98. </excludes>
  99. </configuration>
  100. </plugin>
  101. <plugin>
  102. <groupId>org.apache.maven.plugins</groupId>
  103. <artifactId>maven-war-plugin</artifactId>
  104. <configuration>
  105. <failOnMissingWebXml>false</failOnMissingWebXml>
  106. <archive>
  107. <manifest>
  108. <mainClass>${start-class}</mainClass>
  109. <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
  110. </manifest>
  111. </archive>
  112. </configuration>
  113. </plugin>
  114. <plugin>
  115. <groupId>org.codehaus.mojo</groupId>
  116. <artifactId>exec-maven-plugin</artifactId>
  117. <configuration>
  118. <mainClass>${start-class}</mainClass>
  119. </configuration>
  120. </plugin>
  121. <plugin>
  122. <groupId>org.apache.maven.plugins</groupId>
  123. <artifactId>maven-resources-plugin</artifactId>
  124. <version>2.6</version>
  125. <configuration>
  126. <delimiters>
  127. <delimiter>${resource.delimiter}</delimiter>
  128. </delimiters>
  129. <useDefaultDelimiters>false</useDefaultDelimiters>
  130. </configuration>
  131. </plugin>
  132. <plugin>
  133. <groupId>pl.project13.maven</groupId>
  134. <artifactId>git-commit-id-plugin</artifactId>
  135. <version>2.1.11</version>
  136. <executions>
  137. <execution>
  138. <goals>
  139. <goal>revision</goal>
  140. </goals>
  141. </execution>
  142. </executions>
  143. <configuration>
  144. <verbose>true</verbose>
  145. <dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
  146. <generateGitPropertiesFile>true</generateGitPropertiesFile>
  147. <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
  148. </configuration>
  149. </plugin>
  150. <!-- Support our own plugin -->
  151. <plugin>
  152. <groupId>org.springframework.boot</groupId>
  153. <artifactId>spring-boot-maven-plugin</artifactId>
  154. <executions>
  155. <execution>
  156. <goals>
  157. <goal>repackage</goal>
  158. </goals>
  159. </execution>
  160. </executions>
  161. <configuration>
  162. <mainClass>${start-class}</mainClass>
  163. </configuration>
  164. </plugin>
  165. <!-- Support shade packaging (if the user does not want to use our plugin) -->
  166. <plugin>
  167. <groupId>org.apache.maven.plugins</groupId>
  168. <artifactId>maven-shade-plugin</artifactId>
  169. <dependencies>
  170. <dependency>
  171. <groupId>org.springframework.boot</groupId>
  172. <artifactId>spring-boot-maven-plugin</artifactId>
  173. <version>1.4.1.RELEASE</version>
  174. </dependency>
  175. </dependencies>
  176. <configuration>
  177. <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
  178. <createDependencyReducedPom>true</createDependencyReducedPom>
  179. <filters>
  180. <filter>
  181. <artifact>*:*</artifact>
  182. <excludes>
  183. <exclude>META-INF/*.SF</exclude>
  184. <exclude>META-INF/*.DSA</exclude>
  185. <exclude>META-INF/*.RSA</exclude>
  186. </excludes>
  187. </filter>
  188. </filters>
  189. </configuration>
  190. <executions>
  191. <execution>
  192. <phase>package</phase>
  193. <goals>
  194. <goal>shade</goal>
  195. </goals>
  196. <configuration>
  197. <transformers>
  198. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  199. <resource>META-INF/spring.handlers</resource>
  200. </transformer>
  201. <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
  202. <resource>META-INF/spring.factories</resource>
  203. </transformer>
  204. <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
  205. <resource>META-INF/spring.schemas</resource>
  206. </transformer>
  207. <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
  208. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  209. <mainClass>${start-class}</mainClass>
  210. </transformer>
  211. </transformers>
  212. </configuration>
  213. </execution>
  214. </executions>
  215. </plugin>
  216. </plugins>
  217. </pluginManagement>
  218. </build>
  219. </project>

spring boot默认帮我引入的jdk版本是1.6,如果需要改动,则在自己pom文件中直接重写<java.version>1.6</java.version>即可。

还可以看到它已经帮我们引入了spring-core和commons-logging包

继续往下看可以发现一些属性文件他也帮我定义好了规则,不需要我们自己在xml中定义了,已经一些maven插件的定义这里就不一一说明 ,具体插件是做什么用的,各位可以看下maven的相关知识。

第二个改变的地方是

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. </dependency>
  5.  
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11.  
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  1. spring-boot-starter:核心模块,包括自动配置支持、日志
  1. spring-boot-starter-test :测试模块,包括JUnitHamcrestMockito
  1. spring-boot-starter-web :开发web项目依赖
  2.  
  3. 代码编写

    说了这么多的配置,来让我们创建第一个class
  1. @Controller
  2. @EnableAutoConfiguration
  3. @RequestMapping("/ctr")
  4. public class SampleController
  5. {
  6. @RequestMapping("/getStr")
  7. @ResponseBody
  8. String home(){
  9. return "Hello World";
  10. }
  11.  
  12. @RequestMapping("/info")
  13. @ResponseBody
  14. public Map<String,String> getInfo(){
  15. Map<String, String> map = new HashMap<>();
  16. map.put("name", "jack");
  17. return map;
  18. }
  19.  
  20. public static void main(String[] args)
  21. {
  22. SpringApplication.run(SampleController.class,args);
  23. }
  24. }

到此为止,我们的第一个spring 的web项目就结束了,直接运行main方法,浏览器中输入:http://127.0.0.1:8080/ctr/info 可以看到运行结果  {"name": "jack"}

怎么样,是不是很神奇,没有启动任何的web容器,只需要执行main方法就可以运行我们的web项目了,而且配置就这么简单任性。

Spring Boot 快速入门(一)的更多相关文章

  1. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  2. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

  3. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  4. Spring Boot 快速入门 史上最简单

    1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...

  5. Spring Boot 快速入门(IDEA)

    从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架:SpringBoot帮助开发者快速启动一个Web容器:SpringBoot继承了原有Spring框架的优秀 ...

  6. 笔记61 Spring Boot快速入门(一)

    IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...

  7. Spring Boot 快速入门笔记

    Spirng boot笔记 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...

  8. Spring Boot快速入门(最新)

    本章通过完成Spring Boot基础项目的构建并实现一个简单的Http请求处理,让大家对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性.预计阅读及演练过程将花费约5分钟. ...

  9. Spring Boot快速入门

    安装 安装依赖 maven是一个依赖管理工具,我们利用maven进行构建.创建一个maven项目,在pom.xml里面添加依赖项 <?xml version="1.0" en ...

随机推荐

  1. smarty的学习计划(1)

    1.什么事smarty? 不知道,smarty是一个使用PHP写出来的模板引擎,它提供了逻辑外在内容的分离 2.smarty优点: a.速度:成熟的模板引擎技术 b.编译型:采用smarty编写的程序 ...

  2. 习惯的PHP命名规则

     从C++转PHP也已经很长一段时间了,一直有点代码洁癖,对于文件名,接口名,类名,方法名等都使用严格的驼峰命名法, 但是有时候会犹豫到底用首字母大写区分还是用下划线区分.今天简单总结和规约一下. 1 ...

  3. swift MBProgressHUD加载gif或者apng的动图

    效果图 给MBProgressHUD添加一个分类(extension) extension MBProgressHUD { /// MBProgressHUD 显示加载gif hud方法 /// // ...

  4. oracle数据库常用的99条查询语句(转载)

    1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 fro ...

  5. RPi WiringPi安装使用

    sudo apt-get install git-core git clone git://git.drogon.net/wiringPi   cd wiringPi ./build   使用Exam ...

  6. Ehcache 整合Spring 使用页面、对象缓存(1)

    转自:http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以 ...

  7. javaWeb学习总结(11)- 监听器(Listener)学习

    一.监听器介绍 1.1.监听器的概念 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其 实就是一个实现特定接口的普 ...

  8. Asp.Net Core轻量级Aop解决方案:AspectCore

    什么是AspectCore Project ? AspectCore Project 是适用于Asp.Net Core 平台的轻量级 Aop(Aspect-oriented programming) ...

  9. 新写的高仿Arcmap,要的拿去玩玩

    本想着对所学的ArcGIS Engine开发作一个了结,于是乎写了这么一个仿照Arcmap的程序.我所见过的地理信息系统中,ArcGIS是功能最完善.二次开发最易上手的平台了(当然别提AutoCAD那 ...

  10. 【小练习05】HTML+CSS--淘宝商铺小页面

    要求实现如下效果图: 代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...