springboot的5种读取配置方式(6):总结properties,yml,xml的区别

通过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="{&quot;mod&quot;:&quot;popu_824&quot;}"><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="https://blog.csdn.net/weixin_39220472">
                    <img src="https://profile.csdnimg.cn/D/1/8/3_weixin_39220472" class="avatar_pic" username="weixin_39220472">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/3.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/weixin_39220472" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" 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="https://im.csdn.net/im/main.html?userName=weixin_39220472" 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="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用Jackson XML库来读取XML配置文件中的JSON数据,并使用Spring Data JDBC来连接数据库。 首先,需要在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> ``` 接下来,在application.propertiesapplication.yml文件中配置数据库连接信息: ```yml spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root ``` 然后,在读取XML配置文件中的JSON数据时,可以使用以下代码: ```java @Autowired private ResourceLoader resourceLoader; public void readJsonFromXml() throws IOException { Resource resource = resourceLoader.getResource("classpath:config.xml"); XmlMapper xmlMapper = new XmlMapper(); JsonNode jsonNode = xmlMapper.readTree(resource.getInputStream()).get("database"); String url = jsonNode.get("url").asText(); String username = jsonNode.get("username").asText(); String password = jsonNode.get("password").asText(); // 使用JDBC连接数据库 } ``` 其中,config.xml文件的内容如下: ```xml <config> <database> <url>{"url":"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"}</url> <username>{"username":"root"}</username> <password>{"password":"root"}</password> </database> </config> ``` 通过以上步骤,就可以通过读取XML配置文件中的JSON数据来连接数据库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值