Building an Application with Spring Boot
GETTING STARTED
开发环境
- 一个主流文本编辑器(text editor)或一个集成开发环境(IDE)
- JDK 1.8版本以上
- Gradle 2.3以上或Maven 3.0以上
1 2 3 4 5 6 7
| ~ mvn -v Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00) Maven home: /usr/local/Cellar/maven/3.5.0/libexec Java version: 1.8.0_144, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "mac os x", version: "10.12.5", arch: "x86_64", family: "mac"
|
Build
简单的一个maven入手教程
Building Java Projects with Maven
简单的一个gradle入手教程
Building JavaProjects with Gradle
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| buildscript{ repositories{ mavenCentral() } dependencies{ classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE") } }
apply plugin: 'java' apply plugin: 'idea' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8 targetCompatibility = 1.8
jar { baseName = 'gs-spring-boot' version = '0.1.0' }
repositories { mavenCentral() }
dependencies { compile("org.springframework.boot:spring-boot-starter-web"){ exclude module: "spring-boot-starter-tomcat" } compile("org.springframework.boot:spring-boot-starter-jetty") compile("org.springframework.boot:spring-boot-starter-actuator") testCompile("junit:junit") }
|
Create a Simple Web Application
这里是一个简单的页面控制器逻辑
src/main/java/hello/HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package hello;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping;
@RestController public class HelloController {
@RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; }
}
|
@RestController
表示这个类基于Spring MVC
框架可以响应网络请求。
@RestMapping
匹配\
访问路径到index()
方法。
当一个请求来自客户端或者是命令行(curl
)时,这个方法返回纯文本。
这是因为@RestController
联合了@Controller
和@ResponseBody
两个注解在网络请求中响应了数据而不是视图
Create an Application Class
src/main/java/hello/Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package hello;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean;
@SpringBootApplication public class Application {
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
@Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); }
}; }
}
|
main()
入口主函数,调用了Spring Boot
的SpringApplication.run()
唤起应用程序。
CommandLineRunner
方法被标记为@Bean
注解,会运行在start up
阶段。他打印出了所有Spring Boot
框架所有自动注册的服务组件。
运行应用
Gradle
gs-spring-boot-0.1.0.jar
是build.gradle
中的jar block
的baseName
+version
1
| ./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar
|
Maven
1
| mvn package && java -jar target/gs-spring-boot-0.1.0.jar
|
查看对应jar内容
1
| jar tvf gs-spring-boot-0.1.0.jar
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| 0 Tue Aug 29 10:44:00 CST 2017 META-INF/ 222 Tue Aug 29 10:44:00 CST 2017 META-INF/MANIFEST.MF 0 Tue Aug 29 10:44:00 CST 2017 BOOT-INF/ 0 Tue Aug 29 10:44:00 CST 2017 BOOT-INF/classes/ 0 Mon Aug 28 17:36:04 CST 2017 BOOT-INF/classes/hello/ 2208 Mon Aug 28 17:36:04 CST 2017 BOOT-INF/classes/hello/Application.class 749 Mon Aug 28 17:36:04 CST 2017 BOOT-INF/classes/hello/HelloController.class 0 Tue Aug 29 10:44:00 CST 2017 BOOT-INF/lib/ 2349 Thu Jul 27 07:24:58 CST 2017 BOOT-INF/lib/spring-boot-starter-web-1.5.6.RELEASE.jar 2529 Thu Jul 27 07:25:02 CST 2017 BOOT-INF/lib/spring-boot-starter-jetty-1.5.6.RELEASE.jar 2302 Thu Jul 27 07:25:06 CST 2017 BOOT-INF/lib/spring-boot-starter-actuator-1.5.6.RELEASE.jar 2290 Thu Jul 27 07:24:42 CST 2017 BOOT-INF/lib/spring-boot-starter-1.5.6.RELEASE.jar 725755 Wed Mar 15 13:14:58 CST 2017 BOOT-INF/lib/hibernate-validator-5.3.5.Final.jar 1242477 Mon Jun 12 00:53:20 CST 2017 BOOT-INF/lib/jackson-databind-2.8.9.jar 822491 Thu Jul 20 11:39:20 CST 2017 BOOT-INF/lib/spring-web-4.3.10.RELEASE.jar 915665 Thu Jul 20 11:40:18 CST 2017 BOOT-INF/lib/spring-webmvc-4.3.10.RELEASE.jar 86901 Wed May 31 16:32:34 CST 2017 BOOT-INF/lib/jetty-servlets-9.4.6.v20170531.jar 121362 Wed May 31 16:28:28 CST 2017 BOOT-INF/lib/jetty-webapp-9.4.6.v20170531.jar 34891 Wed May 31 16:38:10 CST 2017 BOOT-INF/lib/websocket-server-9.4.6.v20170531.jar 36848 Wed May 31 16:38:58 CST 2017 BOOT-INF/lib/javax-websocket-server-impl-9.4.6.v20170531.jar 241490 Thu Mar 31 16:58:14 CST 2016 BOOT-INF/lib/apache-el-8.0.33.jar 556647 Thu Jul 27 07:23:38 CST 2017 BOOT-INF/lib/spring-boot-actuator-1.5.6.RELEASE.jar 674636 Thu Jul 27 07:12:50 CST 2017 BOOT-INF/lib/spring-boot-1.5.6.RELEASE.jar 1069471 Thu Jul 27 07:19:22 CST 2017 BOOT-INF/lib/spring-boot-autoconfigure-1.5.6.RELEASE.jar 2312 Thu Jul 27 07:24:42 CST 2017 BOOT-INF/lib/spring-boot-starter-logging-1.5.6.RELEASE.jar 1122794 Thu Jul 20 11:37:08 CST 2017 BOOT-INF/lib/spring-core-4.3.10.RELEASE.jar 273599 Fri Feb 19 13:13:32 CST 2016 BOOT-INF/lib/snakeyaml-1.17.jar 63777 Wed Apr 10 15:02:44 CST 2013 BOOT-INF/lib/validation-api-1.1.0.Final.jar 66023 Wed Mar 15 13:22:08 CST 2017 BOOT-INF/lib/jboss-logging-3.3.1.Final.jar 64982 Tue Sep 27 22:24:16 CST 2016 BOOT-INF/lib/classmate-1.3.3.jar 55784 Sun Jul 03 22:20:36 CST 2016 BOOT-INF/lib/jackson-annotations-2.8.0.jar 282633 Sun Jun 11 17:43:12 CST 2017 BOOT-INF/lib/jackson-core-2.8.9.jar 380667 Thu Jul 20 11:37:16 CST 2017 BOOT-INF/lib/spring-aop-4.3.10.RELEASE.jar 763052 Thu Jul 20 11:37:14 CST 2017 BOOT-INF/lib/spring-beans-4.3.10.RELEASE.jar 1140861 Thu Jul 20 11:37:48 CST 2017 BOOT-INF/lib/spring-context-4.3.10.RELEASE.jar 263371 Thu Jul 20 11:37:28 CST 2017 BOOT-INF/lib/spring-expression-4.3.10.RELEASE.jar 16702 Wed May 31 16:32:14 CST 2017 BOOT-INF/lib/jetty-continuation-9.4.6.v20170531.jar 163922 Wed May 31 16:25:14 CST 2017 BOOT-INF/lib/jetty-http-9.4.6.v20170531.jar 457682 Wed May 31 16:23:48 CST 2017 BOOT-INF/lib/jetty-util-9.4.6.v20170531.jar 128844 Wed May 31 16:24:50 CST 2017 BOOT-INF/lib/jetty-io-9.4.6.v20170531.jar 50392 Wed May 31 16:25:38 CST 2017 BOOT-INF/lib/jetty-xml-9.4.6.v20170531.jar 110502 Wed May 31 16:27:48 CST 2017 BOOT-INF/lib/jetty-servlet-9.4.6.v20170531.jar 199656 Wed May 31 16:37:00 CST 2017 BOOT-INF/lib/websocket-common-9.4.6.v20170531.jar 35362 Wed May 31 16:37:26 CST 2017 BOOT-INF/lib/websocket-client-9.4.6.v20170531.jar 21303 Wed May 31 16:37:50 CST 2017 BOOT-INF/lib/websocket-servlet-9.4.6.v20170531.jar 78056 Wed May 31 16:30:06 CST 2017 BOOT-INF/lib/jetty-annotations-9.4.6.v20170531.jar 160705 Wed May 31 16:38:36 CST 2017 BOOT-INF/lib/javax-websocket-client-impl-9.4.6.v20170531.jar 36611 Fri May 10 13:09:16 CST 2013 BOOT-INF/lib/javax.websocket-api-1.0.jar 309130 Wed Mar 01 20:40:20 CST 2017 BOOT-INF/lib/logback-classic-1.1.11.jar 16515 Thu Mar 16 17:37:30 CST 2017 BOOT-INF/lib/jcl-over-slf4j-1.7.25.jar 4596 Thu Mar 16 17:37:48 CST 2017 BOOT-INF/lib/jul-to-slf4j-1.7.25.jar 23645 Thu Mar 16 17:37:40 CST 2017 BOOT-INF/lib/log4j-over-slf4j-1.7.25.jar 93085 Wed May 31 16:27:16 CST 2017 BOOT-INF/lib/jetty-security-9.4.6.v20170531.jar 41996 Wed May 31 16:36:36 CST 2017 BOOT-INF/lib/websocket-api-9.4.6.v20170531.jar 268784 Wed May 31 16:33:08 CST 2017 BOOT-INF/lib/jetty-client-9.4.6.v20170531.jar 95806 Thu Apr 25 16:52:26 CST 2013 BOOT-INF/lib/javax.servlet-api-3.1.0.jar 55037 Wed May 31 16:29:38 CST 2017 BOOT-INF/lib/jetty-plus-9.4.6.v20170531.jar 26366 Fri Apr 26 19:47:18 CST 2013 BOOT-INF/lib/javax.annotation-api-1.2.jar 53468 Sat Mar 05 14:37:34 CST 2016 BOOT-INF/lib/asm-5.1.jar 47195 Sat Mar 05 14:37:36 CST 2016 BOOT-INF/lib/asm-commons-5.1.jar 475477 Wed Mar 01 20:39:16 CST 2017 BOOT-INF/lib/logback-core-1.1.11.jar 41203 Thu Mar 16 17:36:32 CST 2017 BOOT-INF/lib/slf4j-api-1.7.25.jar 579819 Wed May 31 16:26:32 CST 2017 BOOT-INF/lib/jetty-server-9.4.6.v20170531.jar 29130 Sat Mar 05 14:37:38 CST 2016 BOOT-INF/lib/asm-tree-5.1.jar 0 Tue Aug 29 10:44:00 CST 2017 org/ 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/ 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/boot/ 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/boot/loader/ 2415 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/LaunchedURLClassLoader$1.class 1454 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/PropertiesLauncher$ArchiveEntryFilter.class 1912 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/PropertiesLauncher$PrefixMatchingArchiveFilter.class 4599 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/Launcher.class 1165 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/ExecutableArchiveLauncher$1.class 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/boot/loader/jar/ 2002 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFile$1.class 10016 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/Handler.class 3350 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarEntry.class 1427 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFile$3.class 3104 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/CentralDirectoryEndRecord.class 430 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/CentralDirectoryVisitor.class 1300 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFile$JarFileType.class 10924 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFileEntries.class 12762 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFile.class 1540 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFileEntries$1.class 672 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarURLConnection$1.class 1199 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFile$2.class 262 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarEntryFilter.class 4457 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/AsciiBytes.class 4602 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/CentralDirectoryParser.class 2169 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/Bytes.class 1629 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/ZipInflaterInputStream.class 1967 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarFileEntries$EntryIterator.class 306 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/FileHeader.class 3641 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarURLConnection$JarEntryName.class 9303 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/JarURLConnection.class 5449 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/jar/CentralDirectoryFileHeader.class 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/boot/loader/data/ 1531 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/data/ByteArrayRandomAccessData.class 3549 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class 1862 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/data/RandomAccessDataFile$FilePool.class 1341 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/data/RandomAccessData$ResourceAccess.class 3319 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/data/RandomAccessDataFile.class 551 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/data/RandomAccessData.class 4698 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/LaunchedURLClassLoader.class 1533 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/JarLauncher.class 1468 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/MainMethodRunner.class 1425 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/PropertiesLauncher$1.class 3128 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/ExecutableArchiveLauncher.class 1669 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/WarLauncher.class 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/boot/loader/archive/ 1749 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/JarFileArchive$EntryIterator.class 3792 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/ExplodedArchive$FileEntryIterator.class 1068 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class 1051 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/JarFileArchive$JarFileEntry.class 302 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/Archive$Entry.class 7189 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/JarFileArchive.class 4974 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/ExplodedArchive.class 906 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/Archive.class 1438 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/ExplodedArchive$FileEntryIterator$EntryComparator.class 399 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/Archive$EntryFilter.class 273 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/archive/ExplodedArchive$1.class 18041 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/PropertiesLauncher.class 0 Tue Aug 29 10:44:00 CST 2017 org/springframework/boot/loader/util/ 4887 Thu Jul 27 07:07:34 CST 2017 org/springframework/boot/loader/util/SystemPropertyUtils.class
|
附录
Gradle vs Maven: Feature Comparison Chart