Finding all CLASSPATH resources matching a pattern

前端 未结 4 1644
遥遥无期
遥遥无期 2021-02-05 17:24

I want to read a bunch of text files, by loading them as resources using the context classloader.

URL url = Thread.currentThread()
                .getContextCla         


        
4条回答
  •  攒了一身酷
    2021-02-05 17:46

    Comment from "Binil Thomas" was on the right track, I was looking for confirmation that Spring's PathMatchingResourcePatternResolver could be used from Java Config so that I could give the resulting "Resource" list to the Spring Hibernate SessionFactory.mappingLocations without having to update the list of Hibernate *.hbm.xml files every time a new mapping file was added. I was able to achieve this with the PathMatchingResourcePatternResolver using the below code:

    import org.hibernate.SessionFactory;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
    ...
    ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();   
    Resource [] mappingLocations = patternResolver.getResources("classpath*:mappings/**/*.hbm.xml");
    sessionFactory.setMappingLocations(mappingLocations);
    

    Works like a charm.

提交回复
热议问题