本篇内容介绍了“怎么使用Spring Boot”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
通过上面的 @EnableAutoConfiguration 注解就能根据指定的依赖,自动进行配置。但如果你想关闭某一项自动配置,就需要使用 @SpringBootApplication 下的 exclude 参数来设置。比如,我想关闭 DataSource,代码具体如下:
1@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
能满足企业定制 logo 或项目启动图案。比如,可以借助如下几个网站来生成:
1http://patorjk.com/software/taag2http://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-810server.tomcat.uri-encoding=utf-811# ...
在 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>121314<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>121314<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
-genkey 表示创建一个密钥
-alias httpskey 设置密钥的别名
-keyalg RSA 表示使用的加密算法是 RSA
-keysize 2048 设置密钥的长度
-keystore hello.p12 设置密钥存放位置
-validity 365 设置密钥的有效时间
然后在 application.properties 文件中编辑,具体配置如下:
1server.ssl.key-store=hello.p122server.ssl.key-alias=httpskey3server.ssl.key-store-password=123456
前面也说过,我们的配置文件可以使用 properties 配置和 yaml 配置,项目启动后,它们都会被加载到 Spring 的环境中,如果你要用配置信息的话,直接使用 @Value 注解即可。
但数据注入到属性中,需要注意安全。Spring Boot 使用类型安全配置属性,即使在数据量非常庞大的情况下,将配置文件中的数据注入到 Bean 里也是很方便的。
1user.name=翠花2user.age=183user.address=北京
添加对应的 Bean 类,使用 @ConfigurationProperties
注解来使用配置,通过prefix 属性
来描述要加载的配置文件的前缀,具体如下:
1@Component2@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:803 servlet:4 context-path:/hello5 tomcat:6 uri-encoding:utf-8
还可以自定义配置,具体写法如下:
1user:2 name:翠花3 age:18
对应的代码,跟前面的 User 类一样,具体源码如下:
1@Component2@ConfigurationProperties(prefix="user")3public class User {4 private String name;5 private Integer age;6 // get 和 set 方法...7}
还能设置成集合的样式,集合中是单个值,具体写法如下:
1user:2 name:翠花3 age:184 aihao:5 - 烫头6 - 捏脚7 - Reading
对应的代码,具体源码如下:
1@Component2@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:1811 aihao:12 - 烫头13 - 捏脚14 - Reading
对应的代码,具体源码如下:
1@Component2@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,生产时用 prod2spring.profiles.active=dev
2)在启动类 main 方法中指定
1SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootApp.class);23builder.application().setAdditionalProfiles("prod");45builder.run(args);
3)也可以在项目启动时配置
在我们将项目打成 jar 文件再启动,具体的操作命令如下:
1java -jar springdemo-xxx.jar --spring.profiles.active=prod
来源于:奈学开发者社区-江帅帅
“怎么使用Spring Boot”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69976612/viewspace-2698724/