如何在Spring Boot的XML属性文件中读取属性

时间:2018-04-18 15:11:57

标签: java spring-boot

我创建了一个XML属性文件,其中包含一个sql查询,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="sample.select">
        <![CDATA[
            The SQL query goes here
        ]]>
    </entry>    
</properties>

在我的DAO中,我想获取属性“sample.select”,但我不知道在Spring Boot中是如何完成的,当我搜索它时,我发现我必须添加注释{{1}到Application类,但我想读取DAO类中的ressource文件,而不是Application类。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以在Configuration类中使用您在Configuration中读取的信息创建CompositePollingService。之后,您可以在DAO类中注入Bean。

示例:

@Bean

在你的DAO中你可以注入Bean:

@ImportResource("classpath:my-file.xml")
@Configuration
public class SampleSelectConfig {

    private @Value("#{sample.select}") String select;

    @Bean
    String mySqlFromXml() {
        return select;
    }
}

我无法测试上面的代码,但想法是一样的。