【问题标题】:Spring 3.0 inject files as resourcesSpring 3.0 将文件作为资源注入
【发布时间】:2017-07-11 20:19:48
【问题描述】:

在我的 Spring 3.0 应用程序中,我在 /WEB-INF/dir 中有一些资源。在运行时,我需要其中一些作为InputStream(或其他类型)。我怎样才能找回它们?是否可以将它们作为普通的Resource 注入?

【问题讨论】:

    标签: java spring resources


    【解决方案1】:

    这是一个通过注释最简单的方法:

    import org.springframework.core.io.Resource;
    
    @Value("classpath:<path to file>")
    private Resource cert;
    

    【讨论】:

    • 知道如何加载目录吗?例如,包含 25 个文件的目录“dir”
    • @Andrei_N,我相信我遇到了无法在 jar 文件中工作的问题。如果我作为 Spring Boot 应用程序运行,它可以正常工作,但是如果我将 jar 部署到 aws beanstalk/ec2 我会得到异常。你有关于如何在罐子里处理的建议
    【解决方案2】:

    根据定义,所有ApplicationContexts 都是ResourceLoaders。这意味着它们能够解析在其配置中找到的任何资源字符串。考虑到这一点,您可以使用接受org.springframework.core.io.Resource 的setter 声明您的目标bean。然后在配置目标 bean 时,只需在属性值中使用资源路径即可。 Spring 将尝试将配置中的String 值转换为Resource

    public class Target {
      private Resource resource;
      public void setResource(final Resource resource) {
        this.resource = resource;
      }
    }
    
    //configuration
    <beans>
      <bean id="target" class="Target">
        <property name="resource" value="classpath:path/to/file"/>
      </bean>
    </beans>
    

    【讨论】:

      【解决方案3】:

      你应该可以使用:

      Resource resource = appContext.getResource("classpath:<your resource name>");
      InputStream is = resource.getInputStream();
      

      appContext 是你的 Spring ApplicationContext(特别是 WebApplicationContext,因为你有一个 webapp)

      【讨论】:

      • Resource 接口没有声明getInputStream() 方法,您需要调用new FileInputStream(resource.getFile())
      • 这是不正确的。 Resource继承自接口org.springframework.core.io.InputStreamSource
      • 不好意思,没看到implements
      【解决方案4】:

      这是一个检索类路径资源的完整示例。我用它来抓取具有我不想存储在 Java 类中的非常复杂的查询的 SQL 文件:

      public String getSqlFileContents(String fileName) {
          StringBuffer sb = new StringBuffer();
          try {
              Resource resource = new ClassPathResource(fileName);
              DataInputStream in = new DataInputStream(resource.getInputStream());
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
              while ((strLine = br.readLine()) != null) {
                  sb.append(" " + strLine);
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return sb.toString();
      }
      

      【讨论】:

      • 为什么是栈中的DataInputStream?
      • 真的应该使用Java 8+ try-with-resources。
      【解决方案5】:

      如果您不想引入对 Spring 的依赖,请按照此处详述的方法: Populate Spring Bean's File field via Annotation

      【讨论】:

      • 我看不出该问题的任何答案如何导致对 Spring 的依赖减少...
      • 我同意使用@Value 不是零弹簧依赖。
      • 但是注解是在不违反do-not-repeat-yourself原则的前提下实现spring注入的最佳方式。您推荐的 XML 方法迫使您重复自己。迟早会有人改java文件中的属性名,忘记更新spring bean定义文件。
      • 依赖关系可能不正确,但这是一个有效且非常简单的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多