通过springboot的5种读取配置方式(1):直接读取bean
springboot的5种读取配置方式(2):通过config读取指定文件
springboot的5种读取配置方式(3):通过application.properties读取
springboot的5种读取配置方式(4):通过application.yml读取
springboot的5种读取配置方式(5):通过applicationContext.xml读取
我总结下:
1.config读取:
需要在实体类中声明@Configuration
读取文件是通过@ComponentScan()
比如:
//默认从根目录扫描 @Configuration
//扫描指定包路径 @ComponentScan("springboot.config")
2.读取application文件:
.properties和.yml文件是通过在实体类中加入
@Value注入的
读取文件都是通过@PropertySource()
比如:
.properties文件的格式是key=value
.yml文件格式是key:value
student.name= 小康 student.age=15
#yml文件的配置格式是key:value
student:
name: 小明
age : 25
@Value("KaTeX parse error: Expected 'EOF', got '#' at position 44: …n style="color:#̲cc7832;">privat…{student.age}")
private int age;
//读取resources目录下的application.properties
@PropertySource(“classpath:application.properties”)
//读取resources目录下的application.yml
@PropertySource(“classpath:application.yml”)
需要在pom.xml添加依赖:
<!-- 这个依赖会把配置文件的值注入到@value里面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
3.如果application.properties,application.yml在同一个resources目录下,默认是读取第一个配置文件的
比如:
默认读取的是application.properties文件里的信息。
4.如果存在application.properties或者application.yml的,你把xml命名为application.xml时会报错的:
即使我把application.properties或者application.yml删掉也会报错:
但我把application.xml改名为applicationContext.xml时运行就正常了。
5.applicationContext的读取是用@ImportResource的
//读取resources目录下的applicationContext.xml
@ImportResource(“classpath:applicationContext.xml”)
我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">1</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="">
<img src="" class="avatar_pic" username="weixin_39220472">
<img src="" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="" data-report-click="{"mod":"popu_379"}" target="_blank">灰太狼_cxh</a></span>
</div>
<div class="text"><span>发布了225 篇原创文章</span> · <span>获赞 321</span> · <span>访问量 64万+</span></div>
</div>
<div class="right-message">
<a href="" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>