Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways

Felix Liang 2021-08-08 21:21:56
refined spring boot spring boot


Refinement Spring Boot 03:Spring Boot Configuration file and configuration management , And read the configuration file in three ways

Content abstract : In this paper, Spring Boot Configuration file and configuration management , And three ways to read configuration files , And code demonstration .

stay Spring Boot Before appearance , One Spring There are generally many configuration files in projects , For example, there are configurations and web Server related information web.xml, Have configuration Spring Its own variety application-xxx.xml, One copy xxx Corresponding Sping A function module of , for example MVC and Data Source, And third party plug-ins Such as log4j Etc . Besides , The application itself may also have multiple configuration files , Just counting the number of these configuration files makes people feel dizzy , Let alone manage and configure them .

Spring Boot The arrival of Technology , It greatly simplifies the management and reading of these configuration files . about Spring. Configuration information , Configuration information of the third-party framework it supports , Apply custom configuration information , All integrated in one application.properties In file , There may have been near 10 A new configuration file , Now only one is needed . Don't be like before , The configuration information is scattered in different configuration files of the application . This obviously greatly simplifies the management of configuration information , Of course, it also reduces the probability of error .

1、Web Server configuration

Spring Boot The default to start web Server is Tomcat, The port is 8080, The context is “/”. For these default settings , We can all be in appication.properties Reconfigure .

We're in appication.properties Add two lines of code to

server.port=9090
server.servlet.context-path=/home

image
【 chart 1 Set server properties 】

Press shortcut key Shift + F10 Run the program , Enter the original website http://localhost:8080/hello, Server not found . The reason is that we have used the above two lines of code to modify Tomcat Port number and application context . We can see the result by entering the following URL .

http://localhost:9090/home/hello

image
【 chart 2 Hello, Spring Boot】

2、 use Undertow Replace the default Tomcat The server

Spring Boot Default built-in web Server is Tomcat. in fact , It supports three nested web The server , The other two are Jetty and Undertow, and Undertow Best performance , You can set it to web The server , Replace the other two .

Spring Boot Of spring-boot-starter-web Nested by default Tomcat. When we change to something else web Server time , If replaced with Undertow, Need to put Tomcat To block out . meanwhile , It is also necessary to introduce corresponding web The server corresponds to starter.

Now we're trying to put web The server is switched to Undertow, Modify as follows pom.xml

image
【 chart 3 To configure Undertow 】

The switch found on the Internet web Most server solutions are as follows , But in IDEA Compile and run the program , It's always
Stubbornly use Tomcat, Instead of enabling Undertow. At this time, there is enough reason to suspect that this is IDEA Caused by cache problems , You need to refresh like this IDEA Of Maven project .

image
【 chart 4 IDEA Refresh Maven project 】

Refresh Maven After the project , And then use Maven To compile the program , Double click... As shown in the figure below install.

image
【 chart 5 Maven Install 】

After compiling , Press shortcut key Shift + F10 Run the program , You can see the log output display Undertow It's started successfully .

image
【 chart 6 Undertow Successful startup 】

3、 Read Spring Boot Configuration of

Can be read in the application application.properties Configuration file for , And get the configuration information , For application use . in fact Spring Boot Provides three ways to read configuration files .
The first 1 One way is through Environment class Of getPropery() Method reading .
The first 2 One way is through @Value Annotation to automatically inject attribute values .
The first 3 One way is to automatically inject a set of attributes into a configuration class .

We first in application.properties Add the following four lines of configuration , Then use three methods to read .

app.message1=Hi, Felix!
app.message2=You are really an IT expert.
app.num1=4
app.num2=5

Now modify HelloWorld Of sayHello Method , Use @Value Annotation method to read app.message1, use Enviornment.getProperty() Read app.message2, use @Autowired hold Environment Inject in , You can call its getProperty() The method . The code is as follows :

image
【 chart 7 Environment & @Value】

After starting the program , Successfully read the data of the configuration file in two ways , The output in the browser is as follows :

image
【 chart 8 Felix IT Expert】

The first 3 One way to read the configuration file is to automatically inject a set of attributes into a configuration class , In this case, you need to use annotations @ConfigurationProperties and @Configuration, We read... In this way app.num1 and app.num2 Value , And sum them , Then output the result . Or code .

image
【 chart 9 Configuration Properties 】

AppConfig Class uses two annotations @ConfigurationProperties and @Configuration, stay @ConfigurationProperties The parameter... Is entered in “app”, It's a property app.num1 and app.num2 The prefix of , As you can see from the following code AppConfig It's just an ordinary POJO file , It's just a note .

Next, let's look at how to use AppConfig This class . Now let's create a new summation controller SumController, Using annotations @Autowired hold AppConfig Automatically inject in , You can use it , Is it convenient 、 It's simple .

image
【 chart 10 Summation controller 】

Press Shift + F10 Start the program , Input Sum Method corresponding to url http://localhost:9090/home/sum, You can see the result .

image
【 chart 11 Summation result 】

Okay , The third chapter written by Feixian spacetime Spring Boot To this end , Welcome to forward and follow .

Enjoy coding.

版权声明
本文为[Felix Liang]所创,转载请带上原文链接,感谢
https://javamana.com/2021/08/20210808212132281P.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云