1 Spring boot2需要:maven3.3,jdk1.8以上
2 maven的setting.xml配置:
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
3开始一个Spring boot 应用的编写
创建一个maven工程,在pom.xml中加入依赖,
1)继承spring-boot-starter-parent表明这是一个Springboot 项目。
2)需要实现什么功能时,引入对应的starter依赖即可。
3)父项目的作用是用来做版本依赖管理。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3创建主程序
/**
* 主程序类
* @SpringBootApplication:这是一个SpringBoot应用
*/
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
4编写业务
@RestController
public class HelloController {
@RequestMapping("/hello")
public String handle01(){
return "Hello, Spring Boot 2!";
}
}
5启动应用后,在浏览器中输入http://localhost:8080/hello
说明业务代码需要是主程序所在的包的子包;
6可以通过新增配置文件application.properties,加配置来改变程序的一些配置。比如修改端口号为8888需要,新增配置
server.port=8888
7简化部署,在pom文件中新增配置,将整个项目打包成一个fatjar,然后通过java -jar xxx.jar执行程序。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
注意取消cmd的快速编辑模式。