英文:
Null pointer when auto-wiring in Spring Boot
问题
以下是翻译好的内容:
寻找外部 SDK 类自动连接的标准,我尝试了以下方式,但出现了空指针错误。我在一个 Spring Boot 项目中使用 AmazonDynamoDBClient。所以我认为我有一个配置类,像这样:
@Configuration
public class DynamoDBClientBuilder {
public AmazonDynamoDB amazonDynamoDBClient;
@Bean
public void DynamoDBClientBuilderInitializer() {
amazonDynamoDBClient = this.initiateClient();
}
private AmazonDynamoDB initiateClient() {
return AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration("http://localhost:4569", "us-west-1"))
.build();
}
}
然后我有一个类,在这个 Spring 项目中我想要使用这个客户端,最好在任何方法之外做类似这样的事情:
@Autowired
private DynamoDBClientBuilder db;
private AmazonDynamoDB amazonDynamoDBClient = null;
然后在构造函数或者我们想要使用客户端的地方,可以这样写:
amazonDynamoDBClient = db.amazonDynamoDBClient;
我不太确定为什么会出现空指针错误,似乎配置类在想要使用它的类之前没有被初始化。此外,很有趣听到最佳实践是什么,或者是否有更好的方法来完成这个任务。
谢谢
英文:
Looking for what the standard is for auto-wiring an external sdk class I am trying this way but getting a null pointer.. I am using AmazonDynamoDBClient in a spring boot project. So I think I have my config class that is like this
@Configuration
public class DynamoDBClientBuilder {
public AmazonDynamoDB amazonDynamoDBClient;
@Bean
public void DynamoDBClientBuilderInitializer() {
amazonDynamoDBClient = this.initiateClient();
}
private AmazonDynamoDB initiateClient() {
return AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration("http://localhost:4569", "us-west-1"))
.build();
}
}
Then I have the class where I want to use that client in the spring project, is it best to do something like this outside of any methods
@Autowired
private DynamoDBClientBuilder db;
private AmazonDynamoDB amazonDynamoDBClient = null;
And then this in either the constructor or where we want to use the client
amazonDynamoDBClient = db.amazonDynamoDBClient;
I'm not really sure why i am getting a null pointer it seems the config class isn't getting initiated before the class that wants to use it. Also would be interest to hear what the best practice is or is there a nicer way to do this.
Thanks
答案1
得分: 1
请尝试以下代码:
@Configuration
public class DynamoDBClientBuilder {
@Bean
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration("http://localhost:4569", "us-west-1"))
.build();
}
}
然后像这样注入您的bean:
@Autowired
AmazonDynamoDB amazonDynamoDB;
或者使用构造函数(推荐):
private final AmazonDynamoDB amazonDynamoDB;
@Autowired
public MyClass(AmazonDynamoDB amazonDynamoDB) {
this.amazonDynamoDB = amazonDynamoDB;
}
请注意,您的配置类必须包含组件扫描,以便能够在Spring上下文中创建bean。
英文:
Try this:
@Configuration
public class DynamoDBClientBuilder {
@Bean
public AmazonDynamoDB amazonDynamoDB() {
return AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration("http://localhost:4569", "us-west-1"))
.build();
}
}
And then inject your bean like this:
@Autowired
AmazonDynamoDB amazonDynamoDB;
or with constructor (recommended):
private final AmazonDynamoDB amazonDynamoDB;
@Autowired
public MyClass(AmazonDynamoDB amazonDynamoDB) {
this.amazonDynamoDB = amazonDynamoDB;
}
And notice that your config calss must be covered with component scan to be able to create bean in spring context
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论