我可以在Spring Boot应用程序中使用xml属性文件而不是application.properties吗?(Can I use xml properties file instead of ap

编程入门 行业动态 更新时间:2024-10-11 21:32:09
我可以在Spring Boot应用程序中使用xml属性文件而不是application.properties吗?(Can I use xml properties file instead of application.properties in a Spring Boot application?)

在我们的Spring Boot应用程序中 ,我们使用application.properties ,但是Ops团队要求使用基于XML的属性文件。 格式很简单。 如果apllication.properties看起来像:

com.mail.host=mail_host db.connection.port=1521

相应的xml应该是:

<XML> <com> <mail> <host> mail_host </host> </mail> </com> <db> <connection> <port> 1521 </port> </connection> </db> </XML>

我们实现了这一点,但Spring Boot仍在寻找application.properties以获取其特定属性。 这有点不方便。 我们希望将所有属性放在一个位置,即 - XML文件。

覆盖Spring Boot默认行为的最佳方法是什么,以使其在XML文件中查找属性? (当然,它需要一些自定义代码,因为Spring Boot理解简单的属性格式(如key = value )和YAML格式,但没有XML。)

In our Spring Boot application we use application.properties, but there's a requirement from the Ops team to use XML based properties file. The format is simple. If apllication.properties looks like:

com.mail.host=mail_host db.connection.port=1521

the corresponding xml should be:

<XML> <com> <mail> <host> mail_host </host> </mail> </com> <db> <connection> <port> 1521 </port> </connection> </db> </XML>

We implemented that, but Spring Boot is still looking for the application.properties in order to get its specific properties. This is somewhat inconvenient. We want to have all the properties in one place, i.e. - the XML file.

What's the best way of overriding the default behavior of Spring Boot, to make it look for the properties in the XML file? (Of course, it will need some custom code, because Spring Boot understands simple property format (like key=value) and the YAML format, but no XML.)

最满意答案

Spring Boot不仅从扩展名为.properties和.yaml的文件中读取属性,还从.xml中读取属性。 而不是application.properties只是以下列格式在资源文件夹中提供application.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Application properties in XML format</comment> <entry key="com.mail.host">mail_host</entry> <entry key="db.connection.port">1521</entry> </properties>

在您的类文件中,您可以简单地设置值,就像我们对任何属性或yaml文件一样,即@Value("${com.mail.host}")将在它将注释的任何实例变量上设置相应的值。

The problem is not to write our own XML parser. We already have that. The question is how to make Spring Boot read the specific properties from the XML file, because SB tries to read them automatically from the application.properties when the app starts and we can't make it use our custom XML parser.

Appeared to be easier than I thought. When the app starts, our code invokes a static initializer method that reads the props from the XML file using our custom parser. It sets the Spring Boot specific properties as system ones, by calling:

System.setProperty(...)

Later on, the Spring Boot is able to get these prop values, just as if they are given in the application.properties.

更多推荐

本文发布于:2023-07-14 19:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   而不是   属性   文件   Boot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!