1
votes

I have created an XML properties file which contains an sql query as following :

<?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>

In my DAO I want to get the property "sample.select", but I don't know how is that done in Spring Boot, when I googled about it I found that I have to add the annotation @ImportResource("classpath:my-file.xml") to the Application class, but I want to read the ressource file inside my DAO class and not the Application class.

How can I solve this ?

1

1 Answers

1
votes

You can create a @Bean in your Configuration class with the informations that you read in the Configuration. After that, you can inject the Bean in the DAO class.

Example:

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

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

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

And in your DAO you can inject the Bean:

@Component
class YourDAO {

    @Autowired
    private String mySqlFromXml;

}

I couldn't test the code above, but the idea is the same.