1、idea创建一个maven工程
2、xml下添加springboot父工程管理依赖配置、添加springboot框架启动器
3、主启动类
4、hello controller类
5、直接run主启动类,访问http://localhost:8080/hello?name=Catalina
<?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.example</groupId>
<artifactId>springboot01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!--所有springboot项目都需要继承springboot父工程
父工程管理所有依赖配置。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<!--增加springboot框架启动器,就是所依赖的jar包
web启动器:增加和web开发相关所有jar包
-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Application
package com.example.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 每一个springboot项目都需要一个主启动类
*/
@SpringBootApplication //声明当前应用是一个springboot应用
public class Application {
public static void main(String[] args) {
//启动tomcat,运行springboot应用
SpringApplication.run(Application.class,args);
}
}
controller
package com.example.springboot.controller;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//所有组件类,都应该存放在主启动类application那个类所在的包,或子包下。
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(String name){
return "Hello, Spring Boot!!!!" + name;
}
}
查看默认的自定义配置文件命名格式
<artifactId>spring-boot-starter-parent</artifactId>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
查看默认端口号
{
"name": "server.port",
"defaultValue": 8080
},
修改端口号 application.properties
server.port-8899
运行显示端口号修改为8899
INFO 89131 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer
: Tomcat started on port(s): 8899 (http) with context path ''
开发 生产环境配置
# 激活开发环境配置文件
spring:
profiles:
active: pro
#开发环境
spring:
jdbc:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///springboot
username: root
password: root
#生成/产品环境
spring:
jdbc:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///business
username: business
password: business