纯手工创建SpringBoot项目——HelloWorld
【摘要】 Maven 使用约定优于配置的原则
目录用途${basedir}存放pom.xml和所有的子目录${basedir}/src/main/java项目的java源代码${basedir}/src/main/resources项目的资源,比如说property文件,springmvc.xml${basedir}/src/test/java项目的测试类,比如说Junit代码...
Maven 使用约定优于配置的原则
目录 | 用途 |
---|---|
${basedir} | 存放pom.xml和所有的子目录 |
${basedir}/src/main/java | 项目的java源代码 |
${basedir}/src/main/resources | 项目的资源,比如说property文件,springmvc.xml |
${basedir}/src/test/java | 项目的测试类,比如说Junit代码 |
${basedir}/src/test/resources | 测试用的资源 |
${basedir}/src/main/webapp/WEB-INF | web应用文件目录,web项目的信息,比如存放web.xml、本地图片、jsp视图页面 |
${basedir}/target | 打包输出目录 |
${basedir}/target/classes | 编译输出目录 |
${basedir}/target/test-classes | 测试编译输出目录 |
Test.java | Maven只会自动运行符合该命名规则的测试类 |
~/.m2/repository | Maven默认的本地仓库目录位置 |
1.创建maven工程
// 创建项目根据目录
~/Desktop$ mkdir MySpringboot
// 按照上面maven的约定创建目录
~/Desktop$ mkdir -p MySpringboot/src/main/java
~/Desktop$ mkdir -p MySpringboot/src/main/resources
~/Desktop$ mkdir -p MySpringboot/src/test/java
~/Desktop$ mkdir -p MySpringboot/src/test/resources
// 在根目录下创建pom.xml文件
~/Desktop$ touch MySpringboot/pom.xml
// 创建好的maven项目目录结构
~/Desktop$ tree MySpringboot
MySpringboot
├── pom.xml
└── src ├── main │ ├── java │ └── resources └── test ├── java └── resources
7 directories, 1 file
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
2.导入spring boot相关依赖
在pom.xml加入如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wong</groupId> <!--与项目名称一致--> <artifactId>MySpringboot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
spring-boot-starter-web这个场景启动器会把与web开发相关的依赖导进项目,更多场景启动器请参考:springboot场景启动器
3.编写主程序:启动Spring Boot应用
创建包名com.wong(实际上就是目录):
~/Desktop/MySpringboot$ mkdir -p src/main/java/com/wong
- 1
在包com.wong创建主程序MainApplication.java:
package com.wong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication用于标注主程序
*/
@SpringBootApplication
public class MainApplication{ public static void main(String[] args){ // 启动Spring应用 SpringApplication.run(MainApplication.class,args); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
4.编写业务相关的Controller
创建多一个包,用来存放业务控制器:
~/Desktop/MySpringboot$ mkdir -p src/main/java/com/wong/controller
- 1
创建控制器HelloWorldController.java:
package com.wong.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("world")
public class HelloWorldController{ @GetMapping("hi") public String index(){ return "Hello World!"; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
5.测试
使用mvn spring-boot:run命令运行程序:
~/Desktop/MySpringboot$ mvn spring-boot:run
[INFO] Scanning for projects...
...
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-24 22:13:45.941 INFO 20580 --- [ main] com.wong.MainApplication : Starting MainApplication on kyun-HP-348-G3 with PID 20580 (/home/kyun/Desktop/MySpringboot/target/classes started by kyun in /home/kyun/Desktop/MySpringboot)
2020-03-24 22:13:45.944 INFO 20580 --- [ main] com.wong.MainApplication : No active profile set, falling back to default profiles: default
2020-03-24 22:13:46.662 INFO 20580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-03-24 22:13:46.670 INFO 20580 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-03-24 22:13:46.671 INFO 20580 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-03-24 22:13:46.716 INFO 20580 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-03-24 22:13:46.716 INFO 20580 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 720 ms
2020-03-24 22:13:46.852 INFO 20580 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-24 22:13:46.967 INFO 20580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-24 22:13:46.969 INFO 20580 --- [ main] com.wong.MainApplication : Started MainApplication in 1.298 seconds (JVM running for 1.604)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
浏览器输入http://localhost:8080/world/hi:
6.简化部署
完全可以不用打war包,再发布到Tomcat容器里。可以直接打出jar包,然后直接用java -jar <name>.jar命令来运行。
首先,要在pom.xml加入以下插件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
然后执行以下打包命令:
~/Desktop/MySpringboot$ mvn package spring-boot:repackage
- 1
打出来的包在target目录下:
~/Desktop/MySpringboot$ cd target
~/Desktop/MySpringboot/target$ ls
classes maven-status
generated-sources MySpringboot-1.0-SNAPSHOT.jar
generated-test-sources MySpringboot-1.0-SNAPSHOT.jar.original
maven-archiver test-classes
~/Desktop/MySpringboot/target$ java -jar MySpringboot-1.0-SNAPSHOT.jar
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
以上就是创建SpringBoot项目的所有内容。
文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_40763897/article/details/105081785
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)