In general we would like to inject a static variable, as follows:
@Autowired
private static String str;
However, this end result is null.
1, the use of the configuration file method injection
Private Static String from; Public Static String Getfrom () { return from ; } Public Static void Setfrom (String from) { = from ; } class= "Teststatic" > <property name= "from" value= "abc"/> </bean>
2, the use of annotations, but the annotations are written on a non-static method
Private Static String to; Public Static String Getto () { return to ; } @Value ("${mail.to}") publicvoid Setto (String to) { = to ; }
Note: Currently spring annotations do not support static variables and methods, as for reasons: Some say it is because spring is based on the object-level dependency injection, and the use of static variables or classes of what, the expansion of its life cycle, to testing difficulties, so spring does not recommend this. As for any other reason, sorry, no search, cluck, if you have any good reason, can private chat oh, thank you!
Spring injects a static variable