搭建SpringToolSuit4开发环境
2020-08-30 本文已影响0人
CodingCode
SpringToolSuit4包含了eclipse和spring,这样就不用单独下载eclipse和spring避免安装过程中的各种版本问题。
- 下载SpringToolSuit4
选择平台,下来安装即可:
Screen Shot 2020-08-29 at 11.54.44.png
- 创建第一个工程
安装完之后打开,SpringToolSuit4,在左边窗口选择:Create new Spring Starter Project:
Screen Shot 2020-08-29 at 12.12.39.png
下一步,这里注意选择正确的java版本:
Screen Shot 2020-08-29 at 12.15.49.png
再下一步,选择Spring Web
Screen Shot 2020-08-29 at 12.17.55.png
下一步,直到完成
- 添加功能代码
定义个controller:
添加一个新的package:com.example.demo.controller,新controller的类名为DemoController:
Screen Shot 2020-08-29 at 12.22.21.png
修改主类的SpringBootApplication属性:
从:@SpringBootApplication
到:@SpringBootApplication(scanBasePackages = "com.example.demo.controller")
Screen Shot 2020-08-29 at 12.23.40.png
运行:
run.png
打开浏览器访问:http://localhost:8080/test
添加静态页面 src/main/resources/static:
添加文件index.html
Screen Shot 2020-08-29 at 12.29.49.png
访问:http://localhost:8080/index.html,就能看到添加的静态页面内容。
修改服务配置
Screen Shot 2020-08-29 at 12.32.24.png
这是两个例子:
- 把监听端口改为其他值,8080是默认值。
- 然后还可以修改项目的context path,例如图示例子,那么重启server之后前面的访问地址就要变成:
http://localhost:8080/demo/test 和 http://localhost:8080/demo/index.html
- jar包发布
使用SpringToolSuit4开发的另一个好处是tomcat也不需要了,直接二进制jar包发布运行。
Screen Shot 2020-08-29 at 12.37.36.png
生成jar包target/demo-0.0.1-SNAPSHOT.jar,然后可以直接运行:
$ java -jar target/demo-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.3.RELEASE)
2020-08-29 12:40:11.032 INFO 314 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on <user>-mac with PID 314 (/Users/<user>/Documents/workspace-spring-tool-suite-4-4.7.1.RELEASE/demo/target/demo-0.0.1-SNAPSHOT.jar started by <user> in /Users/<user>/Documents/workspace-spring-tool-suite-4-4.7.1.RELEASE/demo)
2020-08-29 12:40:11.040 INFO 314 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-08-29 12:40:12.417 INFO 314 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-29 12:40:12.435 INFO 314 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-29 12:40:12.435 INFO 314 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-29 12:40:12.574 INFO 314 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring embedded WebApplicationContext
2020-08-29 12:40:12.575 INFO 314 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1435 ms
2020-08-29 12:40:12.936 INFO 314 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-29 12:40:13.066 INFO 314 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2020-08-29 12:40:13.225 INFO 314 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/demo'
2020-08-29 12:40:13.240 INFO 314 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.796 seconds (JVM running for 3.39)
一样访问http://localhost:8080/demo/test和http://localhost:8080/demo/index.html。