Original: Https://www.jianshu.com/p/ea477fc9abf7
For example:
public class Utils { @Value("${test.host}") private static String host; @Value("${test.port}") private static String port; ......}
It @Value
is not possible to assign values directly to static variables, you can use the set method:
@Componentpublic class Utils { private static String host; @Value ("${test.host}") public void sethost(String host) {utils.host = host;} private static String port; @Value ("${test.port}") public void setport(String port) {utils.port = port;} ...}
Note that you need to use @Component
annotations.
Slowgo
Links: Https://www.jianshu.com/p/ea477fc9abf7
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.
Use @Value to assign a value to a static variable in Springboot