赞
踩
通过上面的 @EnableAutoConfiguration 注解就能根据指定的依赖,自动进行配置。但如果你想关闭某一项自动配置,就需要使用 @SpringBootApplication 下的 exclude 参数来设置。比如,我想关闭 DataSource,代码具体如下:
- 1@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
-
能满足企业定制 logo 或项目启动图案。比如,可以借助如下几个网站来生成:
- 1http://patorjk.com/software/taag
- 2http://www.network-science.de/ascii/
- 3http://www.kammerl.de/ascii/AsciiSignature.php
-
生成之后,直接将复制好的图案,放到新建的 banner.txt 文件中。运行程序之后,具体显示效果如下:
一般使用 application.properties 或者 application.yml 文件来当作全局配置文件。它能被添加在下面几个目录下,区别是加载的顺序是不同的,具体如下:
- 1项目根目录的 /config 目录下
- 2项目根目录下
- 3类路径的 /config 目录下
- 4类路径下
-
比如,可以在 application.properties 配置文件中,设置端口、请求后缀等内容。具体的配置参数可以参考官网文档第 10 章 Appendices:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/#common-application-properties
在 Spring Boot 应用中,可以内置 Tomcat、Netty、Jetty 等容器。
如果添加了 spring-boot-starter-web 依赖,则项目会默认使用 Tomcat 作为 Web 容器。
针对 Tomcat 可以添加一些配置,具体配置如下:
- 1# 端口
- 2server.port=80
- 3# 错误跳转路径
- 4server.error.path
- 5# session 失效时间
- 6server.servlet.session.timeout
- 7# 项目名称
- 8server.servlet.context-path
- 9# 编码,一般 utf-8
- 10server.tomcat.uri-encoding=utf-8
- 11# ...
-
在 Spring Boot 应用中嵌入 Jetty 的配置很简单,把 spring-boot-starter-web 中的 Tomcat 改成 Jetty 即可,具体配置如下:
- 1
- 2<dependency>
- 3 <groupId>org.springframework.boot</groupId>
- 4 <artifactId>spring-boot-starter-web</artifactId>
- 5 <exclusions>
- 6 <exclusion>
- 7 <groupId>org.springframework.boot</groupId>
- 8 <artifactId>spring-boot-starter-tomcat</artifactId>
- 9 </exclusion>
- 10 </exclusions>
- 11</dependency>
- 12
- 13
- 14<dependency>
- 15 <groupId>org.springframework.boot</groupId>
- 16 <artifactId>spring-boot-starter-jetty</artifactId>
- 17</dependency>
-

Undertow 是红帽公司开发的一款基于 NIO 的高性能 Web 嵌入式服务器,拥有非常好的性能。配置方式具体如下:
- 1
- 2<dependency>
- 3 <groupId>org.springframework.boot</groupId>
- 4 <artifactId>spring-boot-starter-web</artifactId>
- 5 <exclusions>
- 6 <exclusion>
- 7 <groupId>org.springframework.boot</groupId>
- 8 <artifactId>spring-boot-starter-tomcat</artifactId>
- 9 </exclusion>
- 10 </exclusions>
- 11</dependency>
- 12
- 13
- 14<dependency>
- 15 <groupId>org.springframework.boot</groupId>
- 16 <artifactId>spring-boot-starter-undertow</artifactId>
- 17</dependency>
-

使用 JDK 提供的 keytool 工具,可以生成一个数字证书,具体命令如下:
- 1keytool -genkey -alias httpskey -keyalg RSA -keysize 2048 -keystore hello.p12 -validity 365
-
然后在 application.properties 文件中编辑,具体配置如下:
- 1server.ssl.key-store=hello.p12
- 2server.ssl.key-alias=httpskey
- 3server.ssl.key-store-password=123456
-
前面也说过,我们的配置文件可以使用 properties 配置和 yaml 配置,项目启动后,它们都会被加载到 Spring 的环境中,如果你要用配置信息的话,直接使用 @Value 注解即可。
但数据注入到属性中,需要注意安全。Spring Boot 使用类型安全配置属性,即使在数据量非常庞大的情况下,将配置文件中的数据注入到 Bean 里也是很方便的。
- 1user.name=翠花
- 2user.age=18
- 3user.address=北京
-
添加对应的 Bean 类,使用 @ConfigurationProperties 注解来使用配置,通过prefix 属性来描述要加载的配置文件的前缀,具体如下:
- 1@Component
- 2@ConfigurationProperties(prefix="user")
- 3public class User {
- 4 private String name;
- 5 private Integer age;
- 6 private String address;
- 7 // get 和 set 方法...
- 8}
-
YAML 是专门用来写配置文件的语言,非常简洁、强大,类似 JSON。它可用来替换 application.properties 文件。YAML 主要是由 spring-boot-starter-web 依赖模块中的 snakeyaml 依赖进行解析。但它不能使用 @propertySource 注解加载 YAML 文件,否则还要使用 Properties 配置。
举个小案例,具体写法如下:
- 1server:
- 2 port:80
- 3 servlet:
- 4 context-path:/hello
- 5 tomcat:
- 6 uri-encoding:utf-8
-
还可以自定义配置,具体写法如下:
- 1user:
- 2 name:翠花
- 3 age:18
-
对应的代码,跟前面的 User 类一样,具体源码如下:
- 1@Component
- 2@ConfigurationProperties(prefix="user")
- 3public class User {
- 4 private String name;
- 5 private Integer age;
- 6 // get 和 set 方法...
- 7}
-
还能设置成集合的样式,集合中是单个值,具体写法如下:
- 1user:
- 2 name:翠花
- 3 age:18
- 4 aihao:
- 5 - 烫头
- 6 - 捏脚
- 7 - Reading
-
对应的代码,具体源码如下:
- 1@Component
- 2@ConfigurationProperties(prefix="user")
- 3public class User {
- 4 private String name;
- 5 private Integer age;
- 6 private List<String> aihao;
- 7 // get 和 set 方法...
- 8}
-
也能先设置成集合的样式,但集合中是对象,具体写法如下:
- 1shop:
- 2 users:
- 3 - name:翠花
- 4 age:18
- 5 aihao:
- 6 - 烫头
- 7 - 捏脚
- 8 - Reading
- 9 - name:小强
- 10 age:18
- 11 aihao:
- 12 - 烫头
- 13 - 捏脚
- 14 - Reading
-
对应的代码,具体源码如下:
- 1@Component
- 2@ConfigurationProperties(prefix="shop")
- 3public class Users {
- 4 private List<User> users;
- 5 // get 和 set 方法...
- 6}
-
-
- 1public class User {
- 2 private String name;
- 3 private Integer age;
- 4 private List<String> aihao;
- 5 // get 和 set 方法...
- 6}
-
如果我们在项目中,需要频繁在开发环境、测试环境和生产环境中更改大量的配置,会让你怀疑人生,所以我们使用 @Profile 注解来更加简洁进行处理。
具体的命名规则:application-{xxx}.properties,这样就能在不同环境下进行区别配置信息。具体使用步骤如下:
在 resources 目录中,创建 application-dev.properties 和 application-prod.properties 文件,代表开发和生产环境中的配置。
- 1# 开发环境
- 2server.port=8080
-
-
- 1# 生产环境
- 2server.port=80
-
1)在 application.properties 中指定
- 1# 开发时用 dev,生产时用 prod
- 2spring.profiles.active=dev
-
2)在启动类 main 方法中指定
- 1SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootApp.class);
- 2
- 3builder.application().setAdditionalProfiles("prod");
- 4
- 5builder.run(args);
-
3)也可以在项目启动时配置
在我们将项目打成 jar 文件再启动,具体的操作命令如下:
1java -jar springdemo-xxx.jar --spring.profiles.active=prod
来源于:奈学开发者社区-江帅帅
[奈学教育] IT培训机构_java培训_程序员培训-奈学教育官网www.naixuejiaoyu.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。