【问题标题】:Spring is failling to Autowire Azure Storage BlobSpring 无法自动装配 Azure 存储 Blob
【发布时间】:2021-07-19 03:27:27
【问题描述】:

我有一个 Spring Batch,它有一个 Azure 存储 Blob 的写入器。

我正在使用这个 Azure/Spring 依赖项:

azure-spring-boot-starter-storage 3.4.0

在我的应用程序属性中:

azure.storage.accountName=myAccount
azure.storage.accountKey=myKey

然后在我的 BatchConfig 类中,我自动连接了 AZ Storage:

@Autowired
private BlobServiceClientBuilder blobServiceClientBuilder;

private final BlobServiceAsyncClient blobServiceAsyncClient = blobServiceClientBuilder.buildAsyncClient();

现在,当我启动我的应用程序时,我收到 NullPointerException,因为无法实例化 BlobServiceClientBuilder。

Caused by: java.lang.NullPointerException: null
at com.example.dbreader.configuration.BatchConfig.<init>(BatchConfig.java:55) ~[classes/:na]
at com.example.dbreader.configuration.BatchConfig$$EnhancerBySpringCGLIB$$58787751.<init>(<generated>) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_221]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_221]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_221]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_221]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:212) ~[spring-beans-5.3.5.jar:5.3.5]

自动装配 Azure Blob Bean 时我做错了什么?

我正在使用 Spring 资源来实例化 blob 文件的另一件事:

@Value("blob://{containerName}/" + "#{stepExecutionContext['marketName']}")
private Resource blobFile;

【问题讨论】:

  • 您可能必须创建 bean 并将其提供给框架,而不是创建实例并随身携带,该错误也出现在 BatchConfig #55
  • 您需要首先确保正确配置 Azure 存储资源:这里是官方文档的链接:docs.microsoft.com/en-us/java/api/overview/azure/…。请删除 ` + "#{stepExecutionContext['marketName']}"` 看看这是否能解决问题。我怀疑marketName 中的密钥stepExecutionContext 是空的。请分享您的BatchConfig 课程的代码,以便能够有效地帮助您。
  • 我们最近开源了一个简单的基于 Spring Boot 的实用程序库来上传 blob 数据。查看自动接线选项 - github.com/scoperetail-io/commons-azure-storage

标签: spring azure spring-batch azure-storage azure-blob-storage


【解决方案1】:

遇到了同样的问题。看起来它是文档中的一个错误。 Azure.storage.blob-endpoint 属性对于 blobClient 是必需的,对于 fileClient 是可选的。添加

Azure.storage.blob-endpoint= https://<account-name>.blob.core.windows.net

解决问题

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。最后我发现 buildClient() 方法不能放在全局或构造函数中。只需将其放入 GET/POST 方法中即可。财政年度。

       @Autowired
    private BlobServiceClientBuilder blobServiceClientBuilder;
    
    private BlobServiceClient blobServiceClient;
    
    
    
    @GetMapping
    public String readBlobFile() throws IOException {
        blobServiceClient = blobServiceClientBuilder.buildClient();
        AzureStorageResourcePatternResolver storageResourcePatternResolver = new AzureStorageResourcePatternResolver(blobServiceClient);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-28
      • 2019-02-28
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 2019-09-05
      相关资源
      最近更新 更多