目录
SpringBoot整合了Tomcat,让其无需搭载在Tomcat中就可以运行。
该案例搭载热部署:https://blog.csdn.net/Android_Cob/article/details/105576786
以及解决中文乱码和文件读取:
https://blog.csdn.net/Android_Cob/article/details/105577513
目录结构
1.导入坐标(pom)
<?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>
<!--所有的springboot工程都必须继承spring-boot-starter-parent-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>edu.xiaoying</groupId>
<artifactId>SpringBoot_01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--web功能的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署配置 **IDE在设置的Build下的Compile勾选Build project automatically, “Ctrl+shift+alt+/ 选Registry 再选compiler.automake.allow.when.app.running” ** -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2.声明启动类(MySpringBootApplication)
package edu.xiao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*声明这个是一个SpringBoot的引导类*/
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
3.创建控制器(QuickController)
package edu.xiao.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/user")
public class QuickController {
@RequestMapping("/firstMethod")
public @ResponseBody String firstMethod(){
return "SpringBoot入门成功!";
}
}
4.创建文件读取的控制器(QuickConfigurationAnnoController)
注意:IDE默认是GBK,可能有中文乱码,上面博客有解决方式
package edu.xiao.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ConfigurationProperties(prefix = "person")
public class QuickConfigurationAnnoController {
private String name;
private String address;
private Integer age;
@RequestMapping("/ConfigurationMethod")
public @ResponseBody String firstMethod(){
return "age:"+age+",name:"+name+",address:"+address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
5.配置文件
注意:当properties与yml文件都有同一个属性时,properties会覆盖yml的属性
如:(application.properties)
#服务器端口
server.port=8088
server.servlet.context-path=/demo
person.age = 18
person.name = 张三
person.address = 北京
(application.yml)
person:
name: 李四
age: 22
address: 湖南
6.运行MySpringBootApplication后的效果
覆盖了yml的属性
入门效果