V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
shanghai1943
V2EX  ›  问与答

请教 spring 加载二方包指定目录下的文件

  •  
  •   shanghai1943 · Mar 4, 2022 · 1094 views
    This topic created in 1519 days ago, the information mentioned may be changed or developed.

    现用的是 springboot2.0.4 版本,想加载二方包里的 mybatis 资源,目录为 src/main/resources/mappers ,在 mappers 目录有多个 mybatis mapper 文件。

    试过在二方包里用 new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml")的加载方式,拿到的是应用里第一个包含有 mappers 的 jar 包下的资源,而不是当前二方包里的。

    临时的解决办法是用 new Resource[] { new ClassPathResource("/mappers/a.xml") } 的加载方式,缺点是得一个个列出所需的 mapper 文件。

    不知道有没有更好的办法,想加载整个目录,而不用一个个列举。

    谢谢各位大哥。

    2 replies    2022-03-07 09:53:52 +08:00
    burenshiwo
        1
    burenshiwo  
       Mar 4, 2022
    了解下 classpath*
    shanghai1943
        2
    shanghai1943  
    OP
       Mar 7, 2022
    @chenjian026 #1 现在的情况是,某些二方包里已经使用了 new ClassPathResource 这种写法。如果用 classpath*的话,似乎是会把这些也重复加载进来。有没有只加载当前二方包的写法?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   801 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:35 · PVG 04:35 · LAX 13:35 · JFK 16:35
    ♥ Do have faith in what you're doing.