【问题标题】:How do I load a file in Spring Boot MVC?如何在 Spring Boot MVC 中加载文件?
【发布时间】:2019-01-30 04:02:26
【问题描述】:

我找到的所有示例都使用 XML 配置文件。我尝试了以下方法:

    IdSClientConfigurationImpl config = new IdSClientConfigurationImpl("idsclient.properties");
    config.load();

它给出了一个例外

java.io.FileNotFoundException: idsclient.properties (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_152]
        at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_152]
        at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_152]
        at com.cisco.ccbu.common.ids.client.impl.IdSClientConfigurationImpl.load(IdSClientConfigurationImpl.java:82) ~[idsclientlib-11.6.1.jar:na]

我将文件放在与application.properties 相同的位置。

$ find src -name idsclient\*
src/main/resources/idsclient.properties

它在mvn spring-boot:run期间被复制到classes文件夹

$ find target -name idsclient\*
target/classes/idsclient.properties

那么在 Spring Boot 应用程序的上下文中文件的真实路径是什么?

我也试过

IdSClientConfigurationImpl config = new IdSClientConfigurationImpl("classpath:/idsclient.properties");

但它给了

java.io.FileNotFoundException: classpath:\idsclient.properties(文件名、目录名或卷标语法不正确)

【问题讨论】:

  • 顺便说一下,IdSClientConfigurationImpl 只会接受String
  • 你会想阅读this。磁盘上的物理文件和类路径资源(Spring 尝试使用 classpath:... 检索)之间存在非常重要的区别。
  • 提供完整的相对路径名有效,但似乎不可打包。 new IdSClientConfigurationImpl("src/main/resources/idsclient.properties");
  • src/main/resources 是您的开发环境中的一个位置,不要依赖它。将文件放在您可以阅读的特定位置。或者找到一种不同的方式从类路径中加载它。

标签: java spring spring-mvc spring-boot


【解决方案1】:

您可以使用Spring Resources (ClassPathResource)

   //either autowire
   @Value("classpath:idsclient.properties")
   private Resource idsclientResource; 


   //or construct manually
   ClassPathResource idsclientResource = new ClassPathResource("idsclient.properties");

   ...

   IdSClientConfigurationImpl config = new IdSClientConfigurationImpl(idsclientResource.getFile().getPath()); 

【讨论】:

    【解决方案2】:

    要使用您的自定义属性文件,您可以简单地使用 @PropertySource 注释配置类。以下是我自己的一些项目的 AWS S3 客户端工厂类的示例:

    @PropertySource("classpath:aws.properties")
    @Service
    public class AmazonS3ClientFactory implements FactoryBean<AmazonS3> {
    
    
        @Value("${aws.access-key}")
        private String accessKey;
    
        @Value("${aws.pass}")
        private String pass;
    
        @NotNull
        @Override
        public AmazonS3 getObject() throws Exception {
            return  AmazonS3ClientBuilder.standard()
                    .withRegion(Regions.EU_WEST_3)
                    .withCredentials(
                            new AWSStaticCredentialsProvider(
                                    new BasicAWSCredentials(accessKey, pass)
                            )
                    )
                    .build();
        }
    
        @NotNull
        @Override
        public Class<?> getObjectType() {
            return AmazonS3.class;
        }
    
    }
    

    【讨论】:

    • 非常聪明,但我认为这不适用于需要字符串作为文件路径的 Cisco SDK,调用.load(),然后将配置对象传递给另一个客户端对象:client.init(config); .
    • @Chloe Cisco SDK 究竟做了什么?因为上面的内容应该适用于 Spring 的任何情况......这里让我很不确定的是IdSClientConfigurationImpl,这是我以前从未使用过的。是来自 Spring 还是来自您的 Cisco SDK?我不能 100% 确定理解您要使用自定义配置文件做什么......另外,您应该尝试使用 classpath:idsclient.properties 而不是 classpath:/idsclient.properties,这可能会奏效。
    • IdsClientConfigurationImpl 来自思科。 System.out.println(new File("classpath:idsclient.properties").exists()); 打印 false
    • 现在听起来更清楚了,您可能应该准确地说您正在尝试将配置加载到 Cisco SDK 并且它与 Spring 类路径没有直接关系?根据我自己的经验,真正的 java 类路径可能会非常不同,具体取决于您是使用 spring boot 还是使用 tomcat 运行它,并且它可能还取决于您的 tomcat 安装,所以我可能建议您不要在这里使用类路径因为根据您的环境,这可能会有一些奇怪的行为。
    猜你喜欢
    • 2019-08-06
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2021-03-30
    • 2016-10-28
    • 2019-02-26
    • 2012-07-23
    相关资源
    最近更新 更多