In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use spring @ value to load, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to load using spring @ value. Let's take a look at it.
Spring@value loading time
@ value is an annotation used to inject member variables of a class in spring. It injects the value of the configuration from the configuration file. You need to inject this class PropertyPlaceholderConfigurer in the spring configuration. There are several ways to do so, such as:
Classpath*:config/*.properties
The @ Value injection is done after spring injects the default constructor of the class.
Use
Public static T instantiateClass (Constructor ctor, Object...) of org.springframework.beans.BeanUtils Args)
The class that instantiates @ Component. Use this bean factory DefaultListableBeanFactory.
Use
Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (final String beanName, final RootBeanDefinition mbd, final Object [] args)
To create an instance of this bean, there is some data like this in mbd:
Scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null
By default, all bean managed by spring are singletons.
After the instance is constructed, the @ Value annotation will trigger, org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor's inject (Object bean, String beanName, PropertyValues pvs)
This method injects the properties of the bean into it.
After the injection property is complete (line 557 below), spring loads the other bean. @ Value actually uses the @ AutoWire mechanism.
Because spring only loads the constructor of this class during initialization, if you need to modify the value in the configuration file, you can get the bean instance through the spring context and modify the value using set.
@ Value has garbled codes when loading Chinese. You can configure the encoding format in PropertyPlaceholderConfigurer, which can be adjusted in code above, or you can change the Chinese to unicode encoding.
Talk about the problem with @ Value injection @ Value set the default value
When using @ Value annotation to automatically inject variables, a common problem is that we may report an error in the entire project because we forget to set this parameter in the configuration parameter. In fact, we can set a default value if the configuration parameter is not found when we inject the variable that is annotated by @ Value, which is equivalent to a solution:
There is no usage of default values:
@ Value ("${spring.port}") private String port
Increase the use of default values:
@ Value ("${spring.port:8080}") private String port
What needs to be noted here is:
1. Even if the value of your configuration parameter is String, you don't need to put double quotation marks.
two。 Variables modified by static will fail to be injected through @ Value
Error when using @ Value to inject configuration values
Consider defining a bean of type'java.lang.String' in your configuration.
This is because, in the same class, I quoted another annotation @ AllArgsConstructor.
Later, I deleted @ AllArgsConstructor, leaving only @ Value. The project starts normally.
Conclusion: @ AllArgsConstructor and @ Value annotations cannot be used at the same time
This is the end of the article on "how to load using spring @ value". Thank you for reading! I believe you all have some knowledge about "how to use spring @ value to load". If you want to learn more, 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.
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.