2015-02-10 52 views
0

我在我的彈簧應用程序的類路徑中有一個目錄。如何使用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. 
     } 
    } 
} 
+1

看一看http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/ core/io/support/PathMatchingResourcePatternResolver.html – fateddy 2015-02-10 02:04:19

+0

fateddy:任何代碼示例? – riship89 2015-02-11 18:45:12

+0

找到下面的答案代碼示例。如果這解決了您的問題,請接受答案。 – fateddy 2015-05-09 18:14:22

回答

0

PathMatchingResourcePatternResolver能夠裝載使用特殊classpath*:前綴和/或內部Ant風格的正則表達式的資源。例如。加載匹配的*.sql後綴的類路徑中的所有資源,嘗試下面的代碼片段:

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