【发布时间】: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