Injecting Values into Static Fields in Spring: Techniques and Examples
This tutorial explains why Spring cannot inject @Value directly into static fields and presents three practical solutions—setter injection, constructor injection, and @PostConstruct initialization—along with code examples and a test case to verify successful static value injection.
1. Overview
In this tutorial we explore how to inject values into static fields in the Spring framework, a special but practical scenario for centralized global configuration management.
2. Problem Background
Typically the @Value annotation loads configuration from a property file into an instance field, for example:
name = Inject a value to a static fieldAnd the value can be injected into an instance variable:
@Value("${name}")
private String name;When attempting to apply @Value directly to a static field, the field remains null because Spring does not support direct injection on static fields.
@Component
public class StaticPropertyHolder {
@Value("${name}")
private static String STATIC_NAME_INJECTED_ON_FIELD;
public static String getStaticNameInjectedOnField() {
return STATIC_NAME_INJECTED_ON_FIELD;
}
}3. Reason Analysis
Spring's dependency injection mechanism is fundamentally instance‑based. Even if a class is marked with @Component , Spring does not allow direct injection into static fields because static fields belong to the class’s global state, while dependency injection targets object instances.
4. Solutions
4.1 Setter Method Injection
Define a non‑static setter method annotated with @Value and assign the incoming value to the static field inside the setter.
@Component
public class StaticPropertyHolder {
private static String STATIC_NAME;
// Use @Value on the setter method
@Value("${name}")
public void setStaticName(String name) {
STATIC_NAME = name;
}
public static String getStaticName() {
return STATIC_NAME;
}
}Spring detects the @Value annotation, creates an instance of StaticPropertyHolder , calls setStaticName , and the static field receives the configuration value.
4.2 Constructor Injection
Although less common for static fields, constructor injection can also be used. Spring passes the configuration value as a constructor argument and assigns it to the static field during object creation.
@Component
public class StaticPropertyHolder {
private static String STATIC_NAME;
@Value("${name}")
public StaticPropertyHolder(String name) {
STATIC_NAME = name;
}
public static String getStaticName() {
return STATIC_NAME;
}
}4.3 @PostConstruct Initialization Method
For more complex initialization logic, combine @Value on an instance field with a method annotated by @PostConstruct to copy the value to the static field after the bean is fully constructed.
@Component
public class StaticPropertyHolder {
private static String STATIC_NAME;
@Value("${name}")
private String name;
@PostConstruct
public void init() {
STATIC_NAME = name;
}
public static String getStaticName() {
return STATIC_NAME;
}
}5. Verification
A simple JUnit test can confirm that the static field has been correctly injected.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StaticPropertyHolderTest {
@Autowired
private StaticPropertyHolder staticPropertyHolder;
@Test
public void testStaticNameInjection() {
assertEquals("Inject a value to a static field", StaticPropertyHolder.getStaticName());
}
}Running this test verifies that the static field receives the expected configuration value.
6. Summary
This article examined how to inject values into static fields in Spring. Three main approaches were presented: setter method injection, constructor injection, and using a @PostConstruct initialization method. Each method has its own suitable scenarios, and understanding Spring's dependency injection mechanism allows developers to choose the most appropriate solution for their needs.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.