SpringBoot并不是应用服务器,也并没有实现各种规范,也不会自动生成代码。但是,SpringBoot 提供了几个基础而强大的功能,这里简单介绍两个:
1、自动配置: 针对常见的技术和功能提供自动配置,或者简单的个性化配置(*.properites或*.yml)
2、起步依赖: 只配置需要什么功能就能自动引入需要的相关依赖包,不再是按每个jar包的配置
另外,SpringBoot依然可以使用SpringMVC、Mybatis等技术,对于开发人员本身来说可以更专注于实现业务本身的功能。
本系列教程的代码从github可下载 https://github.com/yinww/demo-springboot2.git
一、开发工具准备
1、JDK1.8
2、Maven
3、Eclipse + STS插件
Eclipse菜单Help > Eclipse Marcketplace... ,在输入框中输入STS回车,点击Install按钮按向导完成安装
二、创建demo工程
1、通过 https://start.spring.io/ 创建
左侧的Group输入com.yinww,Artifact输入demo01, 右侧的Search for dependencies 输入框中输入Web, 再输入Thymeleaf,点击“Switch to the full version”可查看更多的选项。点击Generate Project,会自动下载demo001.zip文件。将demo001.zip解压缩,在eclipse中通过Import...的Existing Maven Projects导入demo001工程。
2、通过STS插件创建
菜单 New > Spring Starter Project,Name输入demo001,Group输入com.yinww,Artifact输入demo001,点击Next进入下一页,选择web,点击Finish,工程创建完毕。
三、编写代码
package com.yinww.demo001.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/")
public String hello() {
return "Hello SpringBoot";
}
}
四、运行
1、通过maven插件运行
点击 Run As > Maven build..., 输入目标 spring-boot:run
2、通过STS插件运行
点击 Run As > Spring Boot App 运行应用程序
五、运行结果
上面两种运行方式在控制台的输出是一致的,如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
2018-11-17 11:36:58.896 INFO 9412 --- [ main] com.yinww.demo001.Demo001Application : Starting Demo001Application on yinww-PC with PID 9412 (C:\Users\yinww\Downloads\demo001\target\classes started by yinww in C:\Users\yinww\Downloads\demo001)
2018-11-17 11:36:58.903 INFO 9412 --- [ main] com.yinww.demo001.Demo001Application : No active profile set, falling back to default profiles: default
2018-11-17 11:37:01.155 INFO 9412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-11-17 11:37:01.189 INFO 9412 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-11-17 11:37:01.189 INFO 9412 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-11-17 11:37:01.203 INFO 9412 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Program\Java\jdk1.8.0_172\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\Program\Java\jdk1.7.0_80\bin;D:\Program\nodejs\node_global;D:\tools\unzip\apache-maven-3.5.4\bin;D:\tools\unzip\mysql\mysql-5.7.23-winx64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;D:\Program\TortoiseSVN\bin;d:\Program\Git\cmd;D:\Program\Redis\;D:\Program\nodejs\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Users\yinww\AppData\Roaming\npm;.]
2018-11-17 11:37:01.351 INFO 9412 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-11-17 11:37:01.352 INFO 9412 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2359 ms
2018-11-17 11:37:01.393 INFO 9412 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-11-17 11:37:01.400 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-17 11:37:01.401 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-17 11:37:01.401 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
2018-11-17 11:37:01.401 INFO 9412 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-17 11:37:01.756 INFO 9412 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-17 11:37:02.157 INFO 9412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-17 11:37:02.163 INFO 9412 --- [ main] com.yinww.demo001.Demo001Application : Started Demo001Application in 3.851 seconds (JVM running for 9.874)
在浏览器中访问 http://localhost:8080/
第一个SpringBoot的demo就运行成功了。
六、其他配置
1、如果安装了STS插件会发现eclipse的Ctrl + Shift + O快捷键不能使用了,解决办法:
eclipse菜单 preference -> general -> keys,点击Bindings列进行排序后找到Ctrl+Shift+O,When列的值为In Windows的数据,在界面底部Wehn下拉框修改为 Editing JAVA SOURCE
2、properties属性默认设置为utf-8
Preferences > General > Content Types 右侧界面选择Text > Java Properties File 将底部的Default encoding改为UTF-8,点击Update即可。这样文件编码自动改为了UTF-8,新建properties文件也自动为UTF-8
更多内容可关注公众号: