Modify the problems encountered by the utility class
Q1: How to inject static member variables?
public class ExampleUtil {
private static ExampleClass1 exampleClass1;
@Autowired
private ExampleClass2 exampleClass2;
@Autowired
private ExampleClass3 exampleClass3;
private static ExampleUtil exampleUtil;
}
Copy the code
A1: set injection
@Autowired
public void setExampleClass1(ExampleClass1 exampleClass1){
ExampleUtil.exampleClass1=exampleClass1;
}
Copy the code
Q2: How is exampleUtil initialized?
A2: using the @ PostConstruct
@PostConstruct
public void init(a) {
exampleUtil = this;
// The member variable needs to be reassigned
exampleUtil.exampleClass2 = this.exampleClass2;
}
Copy the code
reference
@postconstruct annotation usage