可以从application.properties读取变量
我正在制作一个春季应用程序,其中我将数据发送到运动界。我有AccessKey
和SecretKey
存储在application.properties中。在服务实现类中,我使用这些变量,但是我会遇到一个错误,即访问密钥不能为null。 productererServiceImpl类:
public class ProducerServiceImpl extends ProducerService {
private static final Logger LOG = LoggerFactory.getLogger(ProducerServiceImpl.class);
@Value(value = "${aws.stream_name}")
private String streamName;
@Value(value = "${aws.region}")
private String awsRegion;
@Value(value = "${aws.access_key}")
private String awsAccessKey;
@Value(value = "${aws.secret_key}")
private String awsSecretKey;
private KinesisProducer kinesisProducer = null;
public ProducerServiceImpl() {
this.kinesisProducer = getKinesisProducer();
}
private KinesisProducer getKinesisProducer() {
if (kinesisProducer == null) {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
KinesisProducerConfiguration config = new KinesisProducerConfiguration();
config.setRegion(awsRegion);
config.setCredentialsProvider(new AWSStaticCredentialsProvider(awsCreds));
config.setMaxConnections(1);
config.setRequestTimeout(6000); // 6 seconds
config.setRecordMaxBufferedTime(5000); // 5 seconds
kinesisProducer = new KinesisProducer(config);
}
return kinesisProducer;
}
我认为原因是,因为在函数之后,构造函数被称为首先,而变量并未从@value分配值。
I am making a Spring application in which I'm sending data to kinesis. I have the accessKey
and secretKey
stored in application.properties. In the service implementation class I am using these variables but im getting an error that access key cannot be null.
The ProducerServiceImpl class:
public class ProducerServiceImpl extends ProducerService {
private static final Logger LOG = LoggerFactory.getLogger(ProducerServiceImpl.class);
@Value(value = "${aws.stream_name}")
private String streamName;
@Value(value = "${aws.region}")
private String awsRegion;
@Value(value = "${aws.access_key}")
private String awsAccessKey;
@Value(value = "${aws.secret_key}")
private String awsSecretKey;
private KinesisProducer kinesisProducer = null;
public ProducerServiceImpl() {
this.kinesisProducer = getKinesisProducer();
}
private KinesisProducer getKinesisProducer() {
if (kinesisProducer == null) {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
KinesisProducerConfiguration config = new KinesisProducerConfiguration();
config.setRegion(awsRegion);
config.setCredentialsProvider(new AWSStaticCredentialsProvider(awsCreds));
config.setMaxConnections(1);
config.setRequestTimeout(6000); // 6 seconds
config.setRecordMaxBufferedTime(5000); // 5 seconds
kinesisProducer = new KinesisProducer(config);
}
return kinesisProducer;
}
I think the reason is that because after the function, the constructor is called first, the variables are not being assigned the value from @Value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在字段上使用
@Value
注释,则Spring将使用 field注入。这意味着它首先需要创建一个productererServiceimpl
(通过调用构造函数)的实例,然后它将使用反射来初始化用@value
>的那些字段。因此,由于注入值之前,请调用您的构造函数,它们将是
null
。有两个基本解决方案,一种是使用构造函数注入:
另一个解决方案是等到春季初始化豆子。这可以通过将逻辑移至用
@postConstruct
and的方法来完成,但是,首选的解决方案是将整个
keneissproducer
设置移动到Spring配置类,并提供它是豆子。由于所有弹簧豆都是单胎,因此您可以摆脱该初始化代码。例如:现在您可以删除服务中的所有代码并使用:
If you're using the
@Value
annotation on fields, Spring will use field injection. This means that it first needs to create an instance ofProducerServiceImpl
(by calling your constructor), and then it will use reflection to initialize those fields annotated with@Value
.So, since your constructor is invoked before the values are injected, they will be
null
.There are two basic solutions, one is to use constructor injection:
The other solution is to wait until Spring has initialized the bean. This can be done by moving the logic to a method annotated with
@PostConstruct
:However, the prefered solution is to move the entire
KinesisProducer
setup to a Spring configuration class, and provide it as a bean. Since all Spring beans are singletons, you can get rid of that initialization code. For example:Now you can delete all that code in your service and use:
我建议将您的
@value
配置属性使用@ConfigurationProperties
将其注入构造函数。I would suggest moving your
@Value
configuration properties to another class using@ConfigurationProperties
and inject that in your constructor.