简体   繁体   中英

spring framework: load all resources from classpath

I have a directory in my classpath in my spring application. How do I load all files in that directory using ResouceLoader.

// bean for test context
public class DatabaseLoader {
    @Autowired
    protected ResourceLoader myLoader;

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @PostConstruct
    public void init() throws IOException, FileNotFoundException {
        Resource[] resources = myLoader.getResource("classpath:fixtures/*.sql");
        //codepopulate in memory db with all test fixtures
        for (Resource r: resources) {
            //populate in memory db with data in this resource.
        }
    }
}

The PathMatchingResourcePatternResolver is able to load the resources using the special classpath*: prefix and/or internal Ant-style regular expressions. Eg. to load all resources on the classpath matching the *.sql suffix try the following snippet:

PathMatchingResourcePatternResolver loader = new PathMatchingResourcePatternResolver();
Resource[] resources = loader.getResources("classpath:/*.sql");
for (Resource resource : resources) {
    // process resource
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM