一、Banner
介绍:springboot运行时欢迎的图标
自定义欢迎图标
在Resource下添加banner.txt,在文本内添加内容即可 运行结果: 定制网站
https://patorjk.com/software/taag
关闭Banner
@SpringBootApplication
public class BannerApplication {
public static void main(String
[] args
) {
SpringApplicationBuilder builder
= new SpringApplicationBuilder(BannerApplication
.class);
SpringApplication build
= builder
.build();
build
.setBannerMode(Banner
.Mode
.OFF
);
build
.run(args
);
}
运行结果:
二、容器
tomcat
pom.xml
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-web
</artifactId
>
</dependency
>
application.properties
# 设置端口号
server
.port
=8081
# 设置上下文路径
server
.servlet
.context
-path
=/lyl
# 设置tomcat的URL的编码
server
.tomcat
.uri
-encoding
=UTF
-8
编写控制器进行访问
jetty
pom.xml 注:要使用jetty容器,必须先去除内置的tomcat。
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-web
</artifactId
>
<!--去除内置tomcat
-->
<exclusions>
<exclusion>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-tomcat
</artifactId
>
</exclusion
>
</exclusions
>
</dependency
>
<!--使用jetty容器
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-jetty
</artifactId
>
</dependency
>
undertow容器 注:与jetty一样
<!--使用undertow容器
-->
<dependency>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-starter
-undertow
</artifactId
>
</dependency
>
三、property
类型安全的属性注入
@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix
= "book")
public class BookSb {
private Long id
;
private String name
;
private String author
;
}
spring的属性注入
@Component
@PropertySource("classpath:book.properties")
public class Book {
@Value("${book.id}")
private Long id
;
@Value("${book.name}")
private String name
;
@Value("${book.author}")
private String author
;
}
四、ymal
ymal配置文件与properties配置文件的区别
ymal配置是有序的,properties配置是无序的自定义的ymal配置暂时不支持使用注解的方式注入到springboot项目中
ymal配置对数组的友好支持
@Component
@ConfigurationProperties(prefix
= "redis")
public class RedisCluster {
private Integer port
;
private List
<String> hosts
;
private List
<Redis> redisList
;
}
public class Redis {
private Integer port
;
private String host
;
}
redis
:
port
: 6379
hosts
:
- 192.168.1.30
- 192.168.1.31
- 192.168.1.32
redisList
:
- port
: 6379
host
: 192.168.1.34
- port
: 6380
host
: 192.168.1.65
六、profile
介绍:不同的环境对应不同的配置文件
切换配置文件
spring
.profiles
.active
=prod