In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In view of the detailed explanation of the use of Spring@Value attribute injection 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 method.
@ Value is one of the more frequently used annotations in projects that use the Spring framework, and its purpose is to assign values corresponding to key in the configuration file to its annotated properties.
@ Value injection support form
The @ value property injection function can be divided into two categories according to the source of the injected content: through the property injection of the configuration file and through the property injection of the non-configuration file.
The injection of the configuration file can be divided into two categories according to the source of the configuration file: one is the properties in the configuration file application.properties, which are automatically loaded by the default Spring Boot, and the other is the properties in the custom configuration file, which need to be loaded first through @ PropertySource.
The types of non-profile injection can be divided into:
Inject a normal string
Inject operating system properties
Injection expression result
Inject other Bean attributes
Inject file resources
Inject URL resources
Profile-based injection
First of all, let's take a look at the data from the injection of the configuration file, whether it's an application.properties or a custom my.properties file that will be loaded by default. For example, attribute values are defined in application.properties in the following form:
User.name=admin
The properties defined in the my.properties configuration file are as follows:
User.password=pwd123
So, the use of @ Value in Bean is as follows:
@ PropertySource ("classpath:my.properties") @ RestControllerpublic class ValueController {/ * get the configuration attribute * / @ Value ("${user.name}") private String name; / * get the configuration attribute * / @ Value ("${user.password}") private String password located in my.properties }
The difference is that in a Spring Boot project, if it is a custom my.properties file, you need to introduce the configuration file in a class through @ PropertySource, and the properties in application.properties are automatically loaded.
At the same time, you can inject not only individual attributes through @ Value, but also arrays and lists. For example, the configuration is as follows:
Tools=car,train,airplane
You can inject in the following ways:
/ * * injection array (automatically split according to ",") * / @ Value ("${tools}") private String [] toolArray;/** * injection list form (automatically split according to "," split) * / @ Value ("${tools}") private List toolList
By default, Spring is split with "," and converted to the corresponding array or list.
Based on non-profile injection
Before using an example to illustrate an instance that is based on non-profile injection attributes, let's take a look at SpEL.
SpEL (Spring Expression Language), the Spring expression language, can query and manipulate data at run time. Using # {...} as the delimiter, all characters in curly braces are considered SpEL.
Let's take a look at the application of specific example scenarios:
/ * * injecting a normal string is equivalent to directly injecting the operating system attribute * / @ Value ("# {systemProperties ['os.name']}") private String systemPropertiesName;/** * into the default value of the attribute * / @ Value ("program new horizon") private String wechatSubscription;/** * into the expression result * / @ Value ("# {T (java.lang.Math). Random () * 100.0}") private double randomNumber / * * inject other Bean attributes: tool * / @ Value ("# {config.tool}") private String tool;/** * injection list of injected config objects (automatically split according to "|") * / @ Value ("# {${words} '.split ('\ |)}") private List numList;/** * inject file resources * / @ Value ("classpath:config.xml") private Resource resourceFile / * inject URL resources * / @ Value ("http://www.choupangxia.com")private URL homePage"
The use of the following scenarios is shown in the above examples:
Injects a string directly, which is equivalent to initializing the string directly when instantiated.
Inject system properties through # {}.
Inject the expression result through # {}.
Inject attributes of other Bean via # {}.
Attributes are injected and segmented through a combination of # {} and ${}.
The file resource is injected to convert the corresponding string value into the corresponding resource file.
Inject URL resources to convert the corresponding URL string into URL.
Among them, it should be noted that:
The fully qualified name of Resource is org.springframework.core.io.Resource.
When the two are used in combination (# {${}'}), pay attention to single quotation marks and not vice versa.
Default value injection
Whether you use # {} or ${} for attribute injection, you need to set the default value when the corresponding value cannot be obtained, which can be set as follows.
/ * if ip is not configured in the property, the default value * / @ Value ("${ip:127.0.0.1}") private String ip;/** * is used. If the value of port is not obtained in the system property, 8888 is used. * / @ Value ("# {systemProperties ['port']?:' 8888'}") private String port
Where ":" is directly used in ${} to set the default value for undefined or empty values, while # {} needs to use "?:" to set the default value for unset properties.
This is the answer to the question about the detailed explanation of Spring@Value attribute injection 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.