使用java编程,就不得不提到久负盛名的Java后端框架 Spring Boot 今天来跟大家说下怎么创建Spring Boot的第一个项目,都需要做哪些准备吧。
首先介绍下 Spring Boot 主要提供了以下功能:
1.为所有基于 Spring的 java开发提供方便快捷的入门体验
2.开箱即用,有自己自定义的配置就是用自己的,没有就使用官方提供的默认的
3.提供了一系列通用的非功能性的功能,包含:
3.1 嵌入式服务器
3.2 安全管理
3.3 健康检测
4.不需要xml配置,且spring cloud 微服务也是基于Spring Boot框架的
所以了解Spring Boot框架对我们了解java及微服务等都是非常好的。
前期准备:
- ide建议使用idea, 可以在网上下载。社区版或者正式版都是可以的。
- 在电脑上配置好java环境,建议用1.8或者8.0的
- 打开idea,然后在安装插件 Spring Assistant
- 下载Maven包并包装,尽量不要使用idea自带的maven包
安装完插件后,就可以创建Spring Boot项目了
创建好后,就会导入 spring boot的各种包。需要等待差不多10分钟左右,有时候idea会出现假死的情况,可以强制退出后重新进入。
所有包都正常导入后,你会发现没法正常运行程序,因为你还没有正确的导入Maven包。
点击idea右边靠边的maven处,会显示maven的选项
点击其中像钳子一样的图标,会进入Maven的设置
在这里需要将Maven的路径设置为你安装的地址;
users settings file可以设置为maven包里的settings文件,也可以自己修改
local resposistory 可以设置为maven包里的resposistory文件
然后就一直next下去
可以在刚才的Maven页面先clean下,然后再compile
clean主要是为了清理不必要的配置垃圾文件,compile是将maven的配置文件都导入进来。
然后我们就准备启动Spring boot
启动的时候可能会遇到一个问题,就是端口号冲突。Spring boot的默认port是8080,而很多软件或者系统的监听端口都是 8080,这时我们可以修改下Spring boot的监听端口。
src->main->resources-> application.properties
写上 server.port = 8004
那么Spring boot的监听接口就是8004了
现在,让我们运行下
@SpringBootApplicationpublic class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args); }
}
/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51980:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/lib/tools.jar:/Users/lg/Desktop/com.guazi.project/demo/target/test-classes:/Users/lg/Desktop/com.guazi.project/demo/target/classes:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-starter-web/2.3.1.RELEASE/spring-boot-starter-web-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-starter/2.3.1.RELEASE/spring-boot-starter-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot/2.3.1.RELEASE/spring-boot-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-autoconfigure/2.3.1.RELEASE/spring-boot-autoconfigure-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-starter-logging/2.3.1.RELEASE/spring-boot-starter-logging-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/apache/logging/log4j/log4j-to-slf4j/2.13.3/log4j-to-slf4j-2.13.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/apache/logging/log4j/log4j-api/2.13.3/log4j-api-2.13.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/yaml/snakeyaml/1.26/snakeyaml-1.26.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-starter-json/2.3.1.RELEASE/spring-boot-starter-json-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/fasterxml/jackson/core/jackson-databind/2.11.0/jackson-databind-2.11.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/fasterxml/jackson/core/jackson-annotations/2.11.0/jackson-annotations-2.11.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/fasterxml/jackson/core/jackson-core/2.11.0/jackson-core-2.11.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.11.0/jackson-datatype-jdk8-2.11.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.11.0/jackson-datatype-jsr310-2.11.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.11.0/jackson-module-parameter-names-2.11.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-starter-tomcat/2.3.1.RELEASE/spring-boot-starter-tomcat-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/apache/tomcat/embed/tomcat-embed-core/9.0.36/tomcat-embed-core-9.0.36.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.36/tomcat-embed-websocket-9.0.36.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-web/5.2.7.RELEASE/spring-web-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-beans/5.2.7.RELEASE/spring-beans-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-webmvc/5.2.7.RELEASE/spring-webmvc-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-aop/5.2.7.RELEASE/spring-aop-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-context/5.2.7.RELEASE/spring-context-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-expression/5.2.7.RELEASE/spring-expression-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-starter-test/2.3.1.RELEASE/spring-boot-starter-test-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-test/2.3.1.RELEASE/spring-boot-test-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/boot/spring-boot-test-autoconfigure/2.3.1.RELEASE/spring-boot-test-autoconfigure-2.3.1.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/jayway/jsonpath/json-path/2.4.0/json-path-2.4.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/net/minidev/json-smart/2.3/json-smart-2.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/net/minidev/accessors-smart/1.2/accessors-smart-1.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/ow2/asm/asm/5.0.4/asm-5.0.4.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/jakarta/xml/bind/jakarta.xml.bind-api/2.3.3/jakarta.xml.bind-api-2.3.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/jakarta/activation/jakarta.activation-api/1.2.2/jakarta.activation-api-1.2.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/assertj/assertj-core/3.16.1/assertj-core-3.16.1.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/hamcrest/hamcrest/2.2/hamcrest-2.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/junit/jupiter/junit-jupiter/5.6.2/junit-jupiter-5.6.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/junit/jupiter/junit-jupiter-api/5.6.2/junit-jupiter-api-5.6.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/junit/platform/junit-platform-commons/1.6.2/junit-platform-commons-1.6.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/junit/jupiter/junit-jupiter-params/5.6.2/junit-jupiter-params-5.6.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/junit/jupiter/junit-jupiter-engine/5.6.2/junit-jupiter-engine-5.6.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/junit/platform/junit-platform-engine/1.6.2/junit-platform-engine-1.6.2.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/mockito/mockito-core/3.3.3/mockito-core-3.3.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/net/bytebuddy/byte-buddy/1.10.11/byte-buddy-1.10.11.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/net/bytebuddy/byte-buddy-agent/1.10.11/byte-buddy-agent-1.10.11.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/mockito/mockito-junit-jupiter/3.3.3/mockito-junit-jupiter-3.3.3.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/skyscreamer/jsonassert/1.5.0/jsonassert-1.5.0.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-core/5.2.7.RELEASE/spring-core-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-jcl/5.2.7.RELEASE/spring-jcl-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/springframework/spring-test/5.2.7.RELEASE/spring-test-5.2.7.RELEASE.jar:/Users/lg/Desktop/com.guazi.project/apache-maven-3.6.1/respository/org/xmlunit/xmlunit-core/2.7.0/xmlunit-core-2.7.0.jar com.example.demo.DemoApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.1.RELEASE)
2020-07-07 19:25:16.556 INFO 12149 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on ligangdeMacBook-Pro.local with PID 12149 (/Users/lg/Desktop/com.guazi.project/demo/target/classes started by lg in /Users/lg/Desktop/com.guazi.project/demo)
2020-07-07 19:25:16.559 INFO 12149 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-07-07 19:25:18.092 INFO 12149 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8004 (http)
2020-07-07 19:25:18.110 INFO 12149 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-07 19:25:18.110 INFO 12149 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-07 19:25:18.276 INFO 12149 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-07 19:25:18.277 INFO 12149 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1637 ms
2020-07-07 19:25:18.511 INFO 12149 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-07 19:25:18.808 INFO 12149 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8004 (http) with context path ''
2020-07-07 19:25:18.835 INFO 12149 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.902 seconds (JVM running for 3.648)
2020-07-07 19:27:56.786 INFO 12149 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
有这些就说明Spring boot已经启动了。
好了,今天先到这里吧