🚀 No, they happen at different points in the Bean lifecycle.
Constructor injection happens during the Bean Instantiation Phase, meaning before the object is fully created
@Component
public class MyService {
private final String configValue;
public MyService(@Value("${app.config}") String configValue) { // ✅ Injected during instantiation
this.configValue = configValue;
System.out.println("Constructor: configValue = " + configValue);
}
}
new MyService(configValue)
).@Value
property is resolved BEFORE the Bean is fully created.⏳ Happens during: Bean Instantiation Phase.
@Value
) Happens After Bean InstantiationField injection (@Value
on fields) happens in the Dependency Injection Phase, after the Bean is created.
@Value
java
CopyEdit
@Component
public class MyService {
@Value("${app.config}")
private String configValue; // ❌ Injected AFTER constructor runs
public MyService() {
System.out.println("Constructor: configValue = " + configValue); // ❌ Will print null!
}
@PostConstruct
public void init() {
System.out.println("PostConstruct: configValue = " + configValue); // ✅ Injected here
}
}
new MyService()
).configValue
is still null.@Value
property.