Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

In this tutorial, we'll see how we can inject a value from a Java properties file to a static field with Spring.

2. Problem

To begin with, let's imagine that we set a property to a properties file:

name = Inject a value to a static field

Afterward, we want to inject its value to an instance variable.

That usually can be done by using the @Value annotation on an instance field:

private String name;

However, when we try to apply it to a static field, we'll find that it will still be null:

private static String NAME_NULL;

That's because Spring doesn't support @Value on static fields.

Now, truthfully, this is an odd position for our code to be in, and we ought to first consider refactoring. But, let's see how we can make this work.

3. Solution

First, let's declare the static variable we want to inject NAME_STATIC.

Afterward, we'll create a setter method, called setNameStatic and annotate it with the @Value annotation:

public class PropertyController {

    private String name;

    private static String NAME_STATIC;

    public void setNameStatic(String name){
        PropertyController.NAME_STATIC = name;

Let's try and make sense of what's happening above.

First, PropertyController, which is a RestController, is being initialized by Spring.

Afterward, Spring searches for the Value annotated fields and methods.

Spring uses dependency injection to populate the specific value when it finds the @Value annotation. However, instead of handing the value to the instance variable, it's handed to the implicit setter instead. This setter then handles the population of our NAME_STATIC value.

4. Conclusion

In this short tutorial, we've looked at how to inject a value from a properties file into a static variable. This is a route we can consider when our attempts to refactor fail.

As always, the code is available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!