Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to inject values into static variables in the use of @ PostConstruct annotations

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to inject values into static variables in the use of @ PostConstruct annotations. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

The use of @ PostConstruct annotations to inject values into static variables

Today, we encountered a problem when writing a tool class. Generally, when defining a tool class, we will define the method in the tool class as a static type, which can be used through the class name. The method name gets the method without instantiating an object to use its internal method, but when some parameters are defined in the configuration file, we need to get these parameters and use @ Value annotation in the utility class method for parameter injection, but @ Value does not support injection into static variables (spring does not recommend declaring variables or objects as static types, because this expands their scope of use. The purpose of spring dependency injection is to inject objects into the container when they are needed, to use them throughout the declaration cycle, and to make it easier for testing to work. Once declared as static, there is no need to generate instances of the object, which makes testing more difficult, and you cannot rely on injection to generate multiple instances with different dependency environments for a given class.

This static field is implicitly shared and is a global global state, which is also not recommended by spring.)

So now how to use the parameters in the tool class, now the injected parameters are non-static, and the method is static, how to use non-static variables in static methods?

Can be used with @ PostConstruct annotations

Talk about the train of thought.

We can inject the value through the non-static variable, and then assign the non-static variable value to the static variable value through the init method defined by @ PostConstruct (here we need to know the order in which the following classes are loaded)

The code is as follows:

@ Componentpublic class BaiduTranslateUtils {private static Logger logger = LoggerFactory.getLogger (BaiduTranslateUtils.class); private static String BAIDU_FROM = "en"; private static String BAIDU_TO = "zh"; private static String BAIDU_DST = "dst"; private static String APP_ID; private static String SECURITY_KEY; @ Value ("${baidu.translate.app_id}") private String app_id @ Value ("${baidu.translate.security_key}") private String security_key; @ PostConstruct public void init () {/ / initialize assignment APP_ID = app_id; SECURITY_KEY = security_key;} public static String getEntozhOfString (String dis) {/ / static method uses static variable System.out.println (APP_ID+SECURITY_KEY) } @ PostConstruct and static variable injection and spring initialization

The @ PostConstruct annotation is provided by Java, not spring, and is used to modify a non-static void method. It runs when the server loads Servlet, and only once. After bean has created an empty object, it starts to assign values to @ Autowire and @ PostConstruct.

@ Componentpublic class SystemConstant {public static String surroundings; @ Value ("${spring.profiles.active}") public String environment; @ PostConstruct public void initialize () {System.out.println ("initialize the environment..."); surroundings = this.environment;} execution order

The first: when a static method is called directly (no new object): the code block (static variables in order) executes-- method execution.

The second way to create an object: execute the static code of the parent class-- execute the static code of the subclass-- execute the constructor of the parent class-- execute the constructor of the subclass (Note: the method modified by @ PostConstruct is executed after the constructor)

Note: both static variables and static code blocks are executed in the order in which the code is written, and this class will execute (including static variables) as long as the static code block is an operation (calling a static method or creating a new class, etc.).

Static variables can also be injected with value annotations, as long as the annotations are liberated on the variable's set method, and the method cannot be static.

@ Componentpublic class SystemConstant {public static String surroundings; @ Value ("${spring.profiles.active}") public String environment; @ Value ("${spring.profiles.active}") public void setSurroundings (String surroundings) {SystemConstant .surprises = surroundings;}} about the spring initialization operation

Implement the ApplicationRunner interface, and then Override the run method of the ApplicationRunner interface

This is how the editor shares how to inject values into static variables in the use of @ PostConstruct annotations. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report