可以从application.properties读取变量

发布于 2025-02-07 22:32:38 字数 1406 浏览 3 评论 0原文

我正在制作一个春季应用程序,其中我将数据发送到运动界。我有AccessKeySecretKey存储在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮茶煮酒煮时光 2025-02-14 22:32:38

如果您在字段上使用@Value注释,则Spring将使用 field注入。这意味着它首先需要创建一个productererServiceimpl(通过调用构造函数)的实例,然后它将使用反射来初始化用@value>的那些字段。

因此,由于注入值之前,请调用您的构造函数,它们将是null

有两个基本解决方案,一种是使用构造函数注入:

public class ProducerServiceImpl extends ProducerService {
    // ... Fields

    // Pass @Value as constructor parameters
    public ProducerServiceImpl(@Value(value = "${aws.access_key}") String awsAccessKey) {
        // Set the fields before calling getKinesisProducer()
        // If you only need the fields to create the producer, you could also just pass them as arguments to the getKinesisProducer() function
        this.awsAccessKey = awsAccessKey;
        this.kinesisProducer = getKinesisProducer();
    }

    // ...
}

另一个解决方案是等到春季初始化豆子。这可以通过将逻辑移至用@postConstruct and的方法来完成,

public class ProducerServiceImpl extends ProducerService {
    // ... Fields

    // Replace the constructor by a method annotated with @PostConstruct
    @PostConstruct
    public void initializeKinesisProducer() {
        this.kinesisProducer = getKinesisProducer();
    }

    // ...
}

但是,首选的解决方案是将整个keneissproducer设置移动到Spring配置类,并提供它是豆子。由于所有弹簧豆都是单胎,因此您可以摆脱该初始化代码。例如:

// Create a new @Configuration class
@Configuration
public class KinesisConfiguration {

    // Create a @Bean method to construct your KinesisProducer
    @Bean
    // Pass all @Value's as parameters
    public KinesisProducer kinesisProducer(@Value("${aws.access_key} String awsAccessKey) {
        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

        return new KinesisProducer(config);
    }
}

现在您可以删除服务中的所有代码并使用:

public class ProducerServiceImpl extends ProducerService { 
    // Inject your KinesisProducer, either through field injection or constructor injection
    @Autowired
    private KinesisProducer kinesisProducer;
}

If you're using the @Value annotation on fields, Spring will use field injection. This means that it first needs to create an instance of ProducerServiceImpl (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:

public class ProducerServiceImpl extends ProducerService {
    // ... Fields

    // Pass @Value as constructor parameters
    public ProducerServiceImpl(@Value(value = "${aws.access_key}") String awsAccessKey) {
        // Set the fields before calling getKinesisProducer()
        // If you only need the fields to create the producer, you could also just pass them as arguments to the getKinesisProducer() function
        this.awsAccessKey = awsAccessKey;
        this.kinesisProducer = getKinesisProducer();
    }

    // ...
}

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:

public class ProducerServiceImpl extends ProducerService {
    // ... Fields

    // Replace the constructor by a method annotated with @PostConstruct
    @PostConstruct
    public void initializeKinesisProducer() {
        this.kinesisProducer = getKinesisProducer();
    }

    // ...
}

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:

// Create a new @Configuration class
@Configuration
public class KinesisConfiguration {

    // Create a @Bean method to construct your KinesisProducer
    @Bean
    // Pass all @Value's as parameters
    public KinesisProducer kinesisProducer(@Value("${aws.access_key} String awsAccessKey) {
        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

        return new KinesisProducer(config);
    }
}

Now you can delete all that code in your service and use:

public class ProducerServiceImpl extends ProducerService { 
    // Inject your KinesisProducer, either through field injection or constructor injection
    @Autowired
    private KinesisProducer kinesisProducer;
}
沙沙粒小 2025-02-14 22:32:38

我建议将您的@value配置属性使用@ConfigurationProperties将其注入构造函数。

I would suggest moving your @Value configuration properties to another class using @ConfigurationProperties and inject that in your constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文