Spring Boot access static resources missing scr/main/resources

前端 未结 8 507
别那么骄傲
别那么骄傲 2020-12-04 16:39

I am working on a Spring Boot application. I need to parse an XML file (countries.xml) on start. The problem is that I do not understand where to put it so that I could acce

8条回答
  •  一个人的身影
    2020-12-04 16:44

    Because java.net.URL is not adequate for handling all kinds of low level resources, Spring introduced org.springframework.core.io.Resource. To access resources, we can use @Value annotation or ResourceLoader class. @Autowired private ResourceLoader resourceLoader;

    @Override public void run(String... args) throws Exception {

        Resource res = resourceLoader.getResource("classpath:thermopylae.txt");
    
        Map words =  countWords.getWordsCount(res);
    
        for (String key : words.keySet()) {
    
            System.out.println(key + ": " + words.get(key));
        }
    }
    

提交回复
热议问题