Especially when designing utility classes, there are times when you want to target static fields to @ Autowired
. In this case, it seems that you should define the setter of the target static field as a non-static method and add @Autowired
to that setter. The following is an example.
public class SampleClass {
private static StaticField staticField;
@Autowired
public void setStaticField(StaticField staticField) {
SampleClass.staticField = staticField;
}
}
However, since it is bad know-how, if you have to repeat the above frequently, it seems that you will need to review the design, such as making the class a singleton. The reality of system development is that the design cannot be reviewed so easily (´ ・ ω ・ `).
Recommended Posts