In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use @ Value to assign values to static variables in SpringBoot, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Use the write method to get the configuration, but get a null.
@ Value ("${test.appKey}") private static String appKey
Only to find that it is not possible to take the configuration in this way to assign values to static variables.
After searching for a wave on the Internet, we can solve this problem by setter method or through the assignment of intermediate variables.
I used the setter method, the code is as follows:
Private static String appKey;@Value ("${test.appKey}") public void setAppKey (String appKey) {TestIt.appKey = appKey;}
It is important to note that you need to remove the static of the setAppKey method (if you use the setter method generated by generate in idea, you will have static like the variable, which you want to remove here)
When you're done, write a test class to simply test whether it works:
@ RunWith (SpringJUnit4ClassRunner.class) @ SpringBootTest@TestPropertySource (properties = {"spring.config.location = classpath:application-dev.yml"}) public class TestIt {private static String appKey; @ Value ("${test.appKey}") public void setAppKey (String appKey) {TestIt.appKey = appKey;} @ Test public void valueTest () {System.out.println ("forTest:" + appKey) }} this is the answer to the question about how to use @ Value to assign values to static variables in SpringBoot. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.