个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaEE
操作系统
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.7.14version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>springboot_quick_startartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot_quick_startname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
spring-boot-starter-web
可以得到它对应的配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.7.14version>
parent>
<artifactId>spring-boot-starter-parentartifactId>
<packaging>pompackaging>
project>
dependencies
:properties
信息: <properties>
<activemq.version>5.16.6activemq.version>
...
properties>
<dependencies>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-amqpartifactId>
<version>${activemq.version}version>
dependency>
<dependency>
<groupId>org.apache.activemqgroupId>
<artifactId>activemq-blueprintartifactId>
<version>${activemq.version}version>
dependency>
...
dependencies>
spring-boot-starter-web
依赖,可以内置一个tomcat服务器<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.7.14version>
<scope>compilescope>
dependency>
@SpringBootApplication
public class SpringbootQuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootQuickStartApplication.class, args);
}
}
exclusion
排除掉Tomcat依赖:<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jettyartifactId>
dependency>
application.properties
server.port=80
application.yml
server:
port: 81
application.yaml
server:
port: 82
注意:
port:
后一定要加空格
application.properties
优先级最高,其次是application.yml
,最低优先级是application.yaml
,一般写项目时用的配置文件是application.yml
在使用以
.yml
或.yaml
为后缀名的配置文件时,可能会出现自动提示功能消失的问题
解决步骤如下:
.yml
(主流).yaml
@Value
读取单个数据,属性名引用方式:${一级属性名.二级属性名…}lesson: SpringBoot
server:
port: 80
books:
name:
subject:
- Java
- 操作系统
- 网络
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${lesson}")
private String lesson;
@Value("${books.subject[0]}")
private String subject_0;
}
Environment
对象@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Environment environment;
@Autowired
private Books books;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println(lesson);
System.out.println(subject_0);
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("books"));
System.out.println(books);
return "hello,spring boot!";
}
}
@Component
@ConfigurationProperties(prefix = "books")
public class Books {
private String name;
private String[] subject[];
}
在使用自定义对象封装指定数据时,可能会遇到警告信息:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
# 设置启用的环境
spring:
profiles:
active: pro
---
# 开发
spring:
profiles: dev
server:
port: 80
---
# 生产
spring:
profiles: pro
server:
port: 81
---
# 测试
spring:
profiles: test
server:
port: 82
application.properties
spring.profiles.active=dev
application-dev.properties
server.port=8080
application-pro.properties
server.port=8081
application-test.properties
server.port=8082
java -jar springboot.jar --spring.profiles.active=test
java -jar springboot.jar --spring.profiles.active=test --server.port=88
<profiles>
<profile>
<id>devid>
<properties>
<prfile.active>devprfile.active>
properties>
profile>
<profile>
<id>proid>
<properties>
<prfile.active>proprfile.active>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>testid>
<properties>
<prfile.active>testprfile.active>
properties>
profile>
profiles>
# 设置启用的环境
spring:
profiles:
active: ${prfile.active}
---
# 开发
spring:
profiles: dev
server:
port: 80
---
# 生产
spring:
profiles: pro
server:
port: 81
---
# 测试
spring:
profiles: test
server:
port: 82
${}
占位符<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>maven-resources-pluginartifactId>
<configuration>
<encoding>UTF-8encoding>
<useDefaultDelimiters>trueuseDefaultDelimiters>
configuration>
plugin>
plugins>
build>