Finding all CLASSPATH resources matching a pattern

前端 未结 4 1653
遥遥无期
遥遥无期 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:30

    You can try corn-cps

     List<URL> resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.*"), new ResourceNameFilter("*.xml"));
    

    Use the dependecy below in your pom.xml

    <dependency>
        <groupId>net.sf.corn</groupId>
        <artifactId>corn-cps</artifactId>
        <version>1.0.1</version>
    </dependency>
    
    0 讨论(0)
  • 2021-02-05 17:32

    Just use:

    @Value("classpath:folder/*.xml")
    Resource[] resources;
    
    0 讨论(0)
  • 2021-02-05 17:34

    Spring supports ant-style class path resource matching.

    http://static.springsource.org/spring/docs/2.5.x/reference/resources.html

    Examples like : classpath:com/mycompany/**/applicationContext.xml, /WEB-INF/*-context.xml

    See if you can use spring for your project. If it is not possible then you can always pull down the source code to see what they are doing, and do that yourself :)

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题