Spring@Value Attribute injection uses method resolution

This article mainly introduces Spring@Value The use method of attribute injection is analyzed. The example code introduced in this paper is very detailed, which has certain reference value for everyone's study or work. Friends in need can refer to it

In projects using the spring framework, @ value is one of the frequently used annotations. Its function is to assign the value corresponding to the key in the configuration file to the attribute marked by it. In daily use, our common functions are relatively simple. This article systematically takes you to learn about the use of @ value.

@Value injection support form

@The value attribute injection function can be divided into two categories according to the content source of injection: attribute injection through configuration file and attribute injection through non configuration file.

The configuration file injection can be divided into two categories according to the source of the configuration file: one is the configuration file application that will be automatically loaded by the default spring boot Properties; The other is the property in the custom configuration file, which needs to be loaded through @ propertysource first.

The types of non profile injection are divided into:

Based on configuration file injection, first of all, the data comes from the injection of configuration file, whether it is the application that will be loaded by default Properties or custom my Properties file. For example, application The form of defining property values in properties is as follows:

user.name=admin

In my The properties defined in the properties configuration file are as follows:

user.password=pwd123

Then, @ value is used in bean as follows:

@PropertySource("classpath:my.properties")
@RestController
public class ValueController {

  /**
   * 获取位于application.properties中配置的属性
   */
  @Value("${user.name}")
  private String name;

  /**
   * 获取位于my.properties中的配置属性
   */
  @Value("${user.password}")
  private String password;

}

The difference is that in the spring boot project, if it is a custom my Properties file, which needs to be introduced into a class through @ propertysource, and application The properties in properties are automatically loaded.

At the same time, you can inject not only a single attribute through @ value, but also array and list forms. For example, the following configuration:

tools=car,train,airplane

Can be injected in the following ways:

/**
 * 注入数组(自动根据","分割)
 */
@Value("${tools}")
private String[] toolArray;

/**
 * 注入列表形式(自动根据","分割)
 */
@Value("${tools}")
private List<String> toolList;

By default, spring will split with "," and convert it into the corresponding array or list.

Non profile based injection

Before using examples to illustrate instances based on non profile injection properties, let's take a look at spel.

Spiel (spring expression language) is the spring expression language, which can query and manipulate data at runtime. Use #{...} As a delimiter, all characters in braces will be considered spiel.

Let's look at the application of specific example scenarios:

/**
 * 注入普通字符串,相当于直接给属性默认值
 */
@Value("程序新视界")
private String wechatSubscription;

/**
 * 注入操作系统属性
 */
@Value("#{systemProperties['os.name']}")
private String systemPropertiesName;

/**
 * 注入表达式结果
 */
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber;

/**
 * 注入其他Bean属性:注入config对象的属性tool
 */
@Value("#{config.tool}")
private String tool;

/**
 * 注入列表形式(自动根据"|"分割)
 */
@Value("#{'${words}'.split('\\|')}")
private List<String> numList;

/**
 * 注入文件资源
 */
@Value("classpath:config.xml")
private Resource resourceFile;

/**
 * 注入URL资源
 */
@Value("http://www.choupangxia.com")
private URL homePage;

The above examples show the use of the following scenarios:

It should be noted that:

Default value injection

Whether you use #{} or ${} for attribute injection, you need to set the default value when the corresponding value cannot be obtained. You can set it in the following ways.

/**
 * 如果属性中未配置ip,则使用默认值
 */
@Value("${ip:127.0.0.1}")
private String ip;

/**
 * 如果系统属性中未获取到port的值,则使用8888。
 */
@Value("#{systemProperties['port']?:'8888'}")
private String port;

":" is directly used to set the default value of undefined or empty values in ${}, while "?:" is required to set the default value of unset properties in ${}.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>