本文大概1000字,阅读大概需要5分钟
**
IDEA使用spring initializer可以快速的创建Spring Boot项目。



resources文件夹中目录结构:
static:保存所有的静态资源;js css images;template:保存所有的模版页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模版引擎(freemarker、thymeleaf)application.properties:Spring Boot应用的配置文件;可以修改一些默认配置;新建如下文件,并写入代码:

package com.example.demo.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/ini")
public String sayHello() {
return "hello spring boot initializer";
}
}
运行项目,可以看到:

如果SpringApplication的默认设置你不是很喜欢,则可以创建一个本地实例并对其进行自定义。例如,如果要关闭横幅,可以这样编写:
package com.example.demo;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
全局配置文件resources下的application.properties,改名字为固定。
application.properties的用法:扁平的k/v格式:
server.port=8081
server.servlet.context-path=/api
application.yml的用法:树形结构:
server:
port: 8080
可以配置开发和生产环境:


在application.properties中指定:spring.profiles.active=dev,启动程序会发现走了开发配置:

在学习springboot的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。