创建spring项目

通过生成初始代码,配置如下

下载好会得到一个.zip文件,解压导入idea就可以开始了,这是基础的项目结构

让我们打开src/main/java/geektime/spring/hello/hellospring/hellospringapplication.java,修改成如下代码

package geektime.spring.hello.hellospring;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@springbootapplication
@restcontroller
public class hellospringapplication {

	public static void main(string[] args) {
		springapplication.run(hellospringapplication.class, args);
	}

	@requestmapping("/hello")
	public string hello() {
		return "hello spring";
	}
}

接着运行我们修改的代码,运行成功后,console会打印如下信息

/library/java/javavirtualmachines/jdk-15.0.1.jdk/contents/home/bin/java -javaagent:/applications/intellij idea ce.app/contents/lib/idea_rt.jar=55568:/applications/intellij idea ce.app/contents/bin -dfile.encoding=utf-8 -classpath /users/lidean/downloads/hello-spring/target/classes:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-actuator/2.5.0/spring-boot-starter-actuator-2.5.0.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter/2.5.0/spring-boot-starter-2.5.0.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot/2.5.0/spring-boot-2.5.0.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.5.0/spring-boot-autoconfigure-2.5.0.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.5.0/spring-boot-starter-logging-2.5.0.jar:/users/lidean/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/users/lidean/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/users/lidean/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.14.1/log4j-to-slf4j-2.14.1.jar:/users/lidean/.m2/repository/org/apache/logging/log4j/log4j-api/2.14.1/log4j-api-2.14.1.jar:/users/lidean/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/users/lidean/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/users/lidean/.m2/repository/org/yaml/snakeyaml/1.28/snakeyaml-1.28.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-actuator-autoconfigure/2.5.0/spring-boot-actuator-autoconfigure-2.5.0.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-actuator/2.5.0/spring-boot-actuator-2.5.0.jar:/users/lidean/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.3/jackson-databind-2.12.3.jar:/users/lidean/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.3/jackson-annotations-2.12.3.jar:/users/lidean/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.3/jackson-core-2.12.3.jar:/users/lidean/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.12.3/jackson-datatype-jsr310-2.12.3.jar:/users/lidean/.m2/repository/io/micrometer/micrometer-core/1.7.0/micrometer-core-1.7.0.jar:/users/lidean/.m2/repository/org/hdrhistogram/hdrhistogram/2.1.12/hdrhistogram-2.1.12.jar:/users/lidean/.m2/repository/org/latencyutils/latencyutils/2.0.3/latencyutils-2.0.3.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.5.0/spring-boot-starter-web-2.5.0.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.5.0/spring-boot-starter-json-2.5.0.jar:/users/lidean/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.12.3/jackson-datatype-jdk8-2.12.3.jar:/users/lidean/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.12.3/jackson-module-parameter-names-2.12.3.jar:/users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.5.0/spring-boot-starter-tomcat-2.5.0.jar:/users/lidean/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.46/tomcat-embed-core-9.0.46.jar:/users/lidean/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.46/tomcat-embed-el-9.0.46.jar:/users/lidean/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.46/tomcat-embed-websocket-9.0.46.jar:/users/lidean/.m2/repository/org/springframework/spring-web/5.3.7/spring-web-5.3.7.jar:/users/lidean/.m2/repository/org/springframework/spring-beans/5.3.7/spring-beans-5.3.7.jar:/users/lidean/.m2/repository/org/springframework/spring-webmvc/5.3.7/spring-webmvc-5.3.7.jar:/users/lidean/.m2/repository/org/springframework/spring-aop/5.3.7/spring-aop-5.3.7.jar:/users/lidean/.m2/repository/org/springframework/spring-context/5.3.7/spring-context-5.3.7.jar:/users/lidean/.m2/repository/org/springframework/spring-expression/5.3.7/spring-expression-5.3.7.jar:/users/lidean/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/users/lidean/.m2/repository/org/springframework/spring-core/5.3.7/spring-core-5.3.7.jar:/users/lidean/.m2/repository/org/springframework/spring-jcl/5.3.7/spring-jcl-5.3.7.jar geektime.spring.hello.hellospring.hellospringapplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::                (v2.5.0)
2021-06-01 23:15:57.484  info 81444 --- [           main] g.s.h.h.hellospringapplication           : starting hellospringapplication using java 15.0.1 on macbook-pro-9.local with pid 81444 (/users/lidean/downloads/hello-spring/target/classes started by lidean in /users/lidean/downloads/hello-spring)
2021-06-01 23:15:57.488  info 81444 --- [           main] g.s.h.h.hellospringapplication           : no active profile set, falling back to default profiles: default
2021-06-01 23:15:58.610  info 81444 --- [           main] o.s.b.w.embedded.tomcat.tomcatwebserver  : tomcat initialized with port(s): 8080 (http)
2021-06-01 23:15:58.626  info 81444 --- [           main] o.apache.catalina.core.standardservice   : starting service [tomcat]
2021-06-01 23:15:58.626  info 81444 --- [           main] org.apache.catalina.core.standardengine  : starting servlet engine: [apache tomcat/9.0.46]
2021-06-01 23:15:58.685  info 81444 --- [           main] o.a.c.c.c.[tomcat].[localhost].[/]       : initializing spring embedded webapplicationcontext
2021-06-01 23:15:58.685  info 81444 --- [           main] w.s.c.servletwebserverapplicationcontext : root webapplicationcontext: initialization completed in 1073 ms
2021-06-01 23:15:59.156  info 81444 --- [           main] o.s.b.a.e.web.endpointlinksresolver      : exposing 1 endpoint(s) beneath base path '/actuator'
2021-06-01 23:15:59.195  info 81444 --- [           main] o.s.b.w.embedded.tomcat.tomcatwebserver  : tomcat started on port(s): 8080 (http) with context path ''
2021-06-01 23:15:59.208  info 81444 --- [           main] g.s.h.h.hellospringapplication           : started hellospringapplication in 2.658 seconds (jvm running for 3.251)
2021-06-01 23:15:59.254  info 81444 --- [           main] o.s.b.a.applicationavailabilitybean      : application availability state livenessstate changed to correct
2021-06-01 23:15:59.256  info 81444 --- [           main] o.s.b.a.applicationavailabilitybean      : application availability state readinessstate changed to accepting_traffic
2021-06-01 23:22:00.698  info 81444 --- [nio-8080-exec-1] o.a.c.c.c.[tomcat].[localhost].[/]       : initializing spring dispatcherservlet 'dispatcherservlet'
2021-06-01 23:22:00.698  info 81444 --- [nio-8080-exec-1] o.s.web.servlet.dispatcherservlet        : initializing servlet 'dispatcherservlet'
2021-06-01 23:22:00.699  info 81444 --- [nio-8080-exec-1] o.s.web.servlet.dispatcherservlet        : completed initialization in 1 ms

从上面输出的信息我们可以知道tomcat初始化部署在了8080端口,下面我们来访问试试

// 调用我们定义的router
$ curl http://127.0.0.1:8080/hello                            
hello spring%
// 记得我们之前生成项目使用的actuator依赖,用于检查项目是否健康
// 若出现down,我们访问url就会失败
$ curl http://127.0.0.1:8080/actuator/health
{"status":"up"}%        

maven替我们打个包

$ mvn clean packeage -dmaven.test.skip
zsh: command not found: mvn

没命令。。。

若有,可跳过

作者用的是macoszsh,需要做下面几步

# 给文件夹权限
$ sudo chown -r root:wheel downloads/apache-maven*
# 移动
$ mv downloads/apache-maven* /opt/apache-maven
# 打开zsh配置文件
$ nano ~/.zshrc
# 新增命令,写入~/.zshrc
$ export path=$path:/opt/apache-maven/bin

搞定后control+x回车搞定~ 重启下终端,检查mvn版本即可

$ zsh
# 检查maven
$ mvn -version
apache maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
maven home: /opt/apache-maven
java version: 15.0.1, vendor: oracle corporation, runtime: /library/java/javavirtualmachines/jdk-15.0.1.jdk/contents/home
default locale: zh_cn_#hans, platform encoding: utf-8
os name: "mac os x", version: "10.15.6", arch: "x86_64", family: "mac"

继续继续,maven打包我们的代码

$ mvn clean package -dmaven.test.skip 
[info] scanning for projects...
[info] 
[info] -----------------< geektime.spring.hello:hello-spring >-----------------
[info] building hello-spring 0.0.1-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info] 
[info] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hello-spring ---
[info] deleting /users/lidean/downloads/hello-spring/target
[info] 
[info] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hello-spring ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] using 'utf-8' encoding to copy filtered properties files.
[info] copying 1 resource
[info] copying 0 resource
[info] 
[info] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hello-spring ---
[info] changes detected - recompiling the module!
[info] compiling 1 source file to /users/lidean/downloads/hello-spring/target/classes
[info] 
[info] --- maven-resources-plugin:3.2.0:testresources (default-testresources) @ hello-spring ---
[info] not copying test resources
[info] 
[info] --- maven-compiler-plugin:3.8.1:testcompile (default-testcompile) @ hello-spring ---
[info] not compiling test sources
[info] 
[info] --- maven-surefire-plugin:2.22.2:test (default-test) @ hello-spring ---
[info] tests are skipped.
[info] 
[info] --- maven-jar-plugin:3.2.0:jar (default-jar) @ hello-spring ---
[info] building jar: /users/lidean/downloads/hello-spring/target/hello-spring-0.0.1-snapshot.jar
[info] 
[info] --- spring-boot-maven-plugin:2.5.0:repackage (repackage) @ hello-spring ---
[info] replacing main artifact with repackaged archive
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time:  2.965 s
[info] finished at: 2021-06-02t00:02:00+08:00
[info] ------------------------------------------------------------------------

可以看到生成的两个.jar包,一个18m一个却3.0k,因为前者有我们所有的依赖(该.jar包可执行),后者是原始的包

下面我们可以通过java -jar去执行这个jar包,可以看到也是暴露在8080端口,我们的程序可以作为一个单独的进程运行在里面

到此这篇关于手把手教你怎么写spring项目的文章就介绍到这了,更多相关spring项目内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!