手把手教你怎么创建spring项目

 更新时间:2021年06月04日 16:29:58   作者:变优秀的小白  
今天教大家怎么写spring项目,文中有非常详细的图文示例及介绍,对正在学习java的小伙伴们很有帮助,需要的朋友可以参考下

创建Spring项目

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

image.png

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

image.png

让我们打开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] ------------------------------------------------------------------------

image.png

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

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

image.png

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

相关文章

  • Java 正则表达式入门详解(基础进阶)

    Java 正则表达式入门详解(基础进阶)

    最近看到很多同学想要学习java正则表达式的一些知识,那么脚本之家小编就为大家介绍一下,其实正则表达式实用性很强,处理大幅文字的时候都需要用得到,语法也大同小异
    2017-10-10
  • java -jar命令的具体使用

    java -jar命令的具体使用

    java -jar命令是一种方便快捷地在命令行中运行Java可执行jar文件的方法,本文主要介绍了java -jar命令的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • 老生常谈Java反射机制(必看篇)

    老生常谈Java反射机制(必看篇)

    下面小编就为大家带来一篇老生常谈Java反射机制(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • mybatis拦截器实现通用权限字段添加的方法

    mybatis拦截器实现通用权限字段添加的方法

    这篇文章主要给大家介绍了关于mybatis拦截器实现通用权限字段添加的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mybatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • Java @Accessors注解图文详解

    Java @Accessors注解图文详解

    @Accessors用于改变@Data生成的getter和setter方法的生成结果,下面这篇文章主要给大家介绍了关于Java @Accessors注解的相关资料,需要的朋友可以参考下
    2023-02-02
  • 详解J2EE开发的网站部署到阿里云服务器的方法

    详解J2EE开发的网站部署到阿里云服务器的方法

    这篇文章主要介绍了详解J2EE开发的网站部署到阿里云服务器的方法,需要的朋友可以参考下
    2018-01-01
  • 深入了解java 8的函数式编程

    深入了解java 8的函数式编程

    函数式编程并不是Java新提出的概念,其与指令编程相比,强调函数的计算比指令的计算更重要;与过程化编程相比,其中函数的计算可以随时调用。下面我们来详细了解一下吧
    2019-06-06
  • logback使用filter过滤日志操作

    logback使用filter过滤日志操作

    这篇文章主要介绍了logback使用filter过滤日志操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Spring boot跨域设置实例详解

    Spring boot跨域设置实例详解

    这篇文章主要介绍了Spring boot跨域设置实例详解,简单介绍了跨域的定义,原因,使用场景及解决方案,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • hibernate多表操作实例代码

    hibernate多表操作实例代码

    这篇文章主要介绍了hibernate多表操作实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02

最新评论